add: New private function for csv/xlsx merge
This commit is contained in:
@@ -16,27 +16,13 @@ function Merge-CISExcelAndCsvData {
|
|||||||
$import = Import-Excel -Path $ExcelPath -WorksheetName $WorksheetName
|
$import = Import-Excel -Path $ExcelPath -WorksheetName $WorksheetName
|
||||||
$csvData = Import-Csv -Path $CsvPath
|
$csvData = Import-Csv -Path $CsvPath
|
||||||
|
|
||||||
# Define a function to create a merged object
|
|
||||||
function CreateMergedObject($excelItem, $csvRow) {
|
|
||||||
$newObject = New-Object PSObject
|
|
||||||
|
|
||||||
foreach ($property in $excelItem.PSObject.Properties) {
|
|
||||||
$newObject | Add-Member -MemberType NoteProperty -Name $property.Name -Value $property.Value
|
|
||||||
}
|
|
||||||
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_Connection' -Value $csvRow.Connection
|
|
||||||
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_Status' -Value $csvRow.Status
|
|
||||||
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_Details' -Value $csvRow.Details
|
|
||||||
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_FailureReason' -Value $csvRow.FailureReason
|
|
||||||
return $newObject
|
|
||||||
}
|
|
||||||
|
|
||||||
# Iterate over each item in the imported Excel object and merge with CSV data
|
# Iterate over each item in the imported Excel object and merge with CSV data
|
||||||
$mergedData = foreach ($item in $import) {
|
$mergedData = foreach ($item in $import) {
|
||||||
$csvRow = $csvData | Where-Object { $_.Rec -eq $item.'recommendation #' }
|
$csvRow = $csvData | Where-Object { $_.Rec -eq $item.'recommendation #' }
|
||||||
if ($csvRow) {
|
if ($csvRow) {
|
||||||
CreateMergedObject -excelItem $item -csvRow $csvRow
|
New-MergedObject -ExcelItem $item -CsvRow $csvRow
|
||||||
} else {
|
} else {
|
||||||
CreateMergedObject -excelItem $item -csvRow ([PSCustomObject]@{Connection=$null;Status=$null; Details=$null; FailureReason=$null })
|
New-MergedObject -ExcelItem $item -CsvRow ([PSCustomObject]@{Connection=$null;Status=$null; Details=$null; FailureReason=$null })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
20
source/Private/New-MergedObject.ps1
Normal file
20
source/Private/New-MergedObject.ps1
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
function New-MergedObject {
|
||||||
|
param (
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[psobject]$ExcelItem,
|
||||||
|
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[psobject]$CsvRow
|
||||||
|
)
|
||||||
|
|
||||||
|
$newObject = New-Object PSObject
|
||||||
|
|
||||||
|
foreach ($property in $ExcelItem.PSObject.Properties) {
|
||||||
|
$newObject | Add-Member -MemberType NoteProperty -Name $property.Name -Value $property.Value
|
||||||
|
}
|
||||||
|
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_Connection' -Value $CsvRow.Connection
|
||||||
|
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_Status' -Value $CsvRow.Status
|
||||||
|
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_Details' -Value $CsvRow.Details
|
||||||
|
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_FailureReason' -Value $CsvRow.FailureReason
|
||||||
|
return $newObject
|
||||||
|
}
|
27
tests/Unit/Private/New-MergedObject.tests.ps1
Normal file
27
tests/Unit/Private/New-MergedObject.tests.ps1
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path
|
||||||
|
$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{
|
||||||
|
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
|
||||||
|
$(try { Test-ModuleManifest $_.FullName -ErrorAction Stop } catch { $false } )
|
||||||
|
}).BaseName
|
||||||
|
|
||||||
|
|
||||||
|
Import-Module $ProjectName
|
||||||
|
|
||||||
|
InModuleScope $ProjectName {
|
||||||
|
Describe Get-PrivateFunction {
|
||||||
|
Context 'Default' {
|
||||||
|
BeforeEach {
|
||||||
|
$return = Get-PrivateFunction -PrivateData 'string'
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'Returns a single object' {
|
||||||
|
($return | Measure-Object).Count | Should -Be 1
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'Returns a string based on the parameter PrivateData' {
|
||||||
|
$return | Should -Be 'string'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user