fix: Fix merging csv when data present

This commit is contained in:
DrIOS
2024-06-17 19:19:07 -05:00
parent aa76de6649
commit d6c500f953
5 changed files with 13 additions and 18 deletions

View File

@@ -16,27 +16,23 @@ function Merge-CISExcelAndCsvData {
) )
process { process {
# Import data from Excel
$import = Import-Excel -Path $ExcelPath -WorksheetName $WorksheetName $import = Import-Excel -Path $ExcelPath -WorksheetName $WorksheetName
# Import data from CSV or use provided object
$csvData = if ($PSCmdlet.ParameterSetName -eq 'CsvInput') { $csvData = if ($PSCmdlet.ParameterSetName -eq 'CsvInput') {
Import-Csv -Path $CsvPath Import-Csv -Path $CsvPath
} else { } else {
$AuditResults $AuditResults
} }
# Iterate over each item in the imported Excel object and merge with CSV data or audit results
$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) {
New-MergedObject -ExcelItem $item -CsvRow $csvRow New-MergedObject -ExcelItem $item -CsvRow $csvRow
} else { } else {
New-MergedObject -ExcelItem $item -CsvRow ([PSCustomObject]@{Connection=$null;Status=$null; Details=$null; FailureReason=$null }) $item
} }
} }
# Return the merged data
return $mergedData return $mergedData
} }
} }

View File

@@ -12,11 +12,13 @@ function New-MergedObject {
$newObject = New-Object PSObject $newObject = New-Object PSObject
foreach ($property in $ExcelItem.PSObject.Properties) { foreach ($property in $ExcelItem.PSObject.Properties) {
$newObject | Add-Member -MemberType NoteProperty -Name $property.Name -Value $property.Value $newObject | Add-Member -MemberType NoteProperty -Name $property.Name -Value $property.Value -Force
} }
$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_Connection' -Value $CsvRow.Connection -Force
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_Details' -Value $CsvRow.Details $newObject | Add-Member -MemberType NoteProperty -Name 'CSV_Status' -Value $CsvRow.Status -Force
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_FailureReason' -Value $CsvRow.FailureReason $newObject | Add-Member -MemberType NoteProperty -Name 'CSV_Details' -Value $CsvRow.Details -Force
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_FailureReason' -Value $CsvRow.FailureReason -Force
return $newObject return $newObject
} }

View File

@@ -24,7 +24,6 @@ function Update-CISExcelWorksheet {
throw "Worksheet '$WorksheetName' not found in '$ExcelPath'" throw "Worksheet '$WorksheetName' not found in '$ExcelPath'"
} }
# Update the worksheet with the provided data # Update the worksheet with the provided data
Update-WorksheetCell -Worksheet $worksheet -Data $Data -StartingRowIndex $StartingRowIndex Update-WorksheetCell -Worksheet $worksheet -Data $Data -StartingRowIndex $StartingRowIndex

View File

@@ -10,7 +10,8 @@ function Update-WorksheetCell {
$firstItem = $Data[0] $firstItem = $Data[0]
$colIndex = 1 $colIndex = 1
foreach ($property in $firstItem.PSObject.Properties) { foreach ($property in $firstItem.PSObject.Properties) {
if ($StartingRowIndex -eq 2 -and $Worksheet.Cells[1, $colIndex].Value -eq $null) { # Update headers if they don't exist or if explicitly needed
if ($Worksheet.Cells[1, $colIndex].Value -ne $property.Name) {
$Worksheet.Cells[1, $colIndex].Value = $property.Name $Worksheet.Cells[1, $colIndex].Value = $property.Name
} }
$colIndex++ $colIndex++

View File

@@ -66,25 +66,22 @@ function Sync-CISExcelAndCsvData {
) )
process { process {
# Verify ImportExcel module is available
$requiredModules = Get-RequiredModule -SyncFunction $requiredModules = Get-RequiredModule -SyncFunction
foreach ($module in $requiredModules) { foreach ($module in $requiredModules) {
Assert-ModuleAvailability -ModuleName $module.ModuleName -RequiredVersion $module.RequiredVersion -SubModuleName $module.SubModuleName Assert-ModuleAvailability -ModuleName $module.ModuleName -RequiredVersion $module.RequiredVersion -SubModuleName $module.SubModuleName
} }
# Merge Excel and CSV data or Audit Results
if ($PSCmdlet.ParameterSetName -eq 'CsvInput') { if ($PSCmdlet.ParameterSetName -eq 'CsvInput') {
$mergedData = Merge-CISExcelAndCsvData -ExcelPath $ExcelPath -WorksheetName $WorksheetName -CsvPath $CsvPath $mergedData = Merge-CISExcelAndCsvData -ExcelPath $ExcelPath -WorksheetName $WorksheetName -CsvPath $CsvPath
} else { } else {
$mergedData = Merge-CISExcelAndCsvData -ExcelPath $ExcelPath -WorksheetName $WorksheetName -AuditResults $AuditResults $mergedData = Merge-CISExcelAndCsvData -ExcelPath $ExcelPath -WorksheetName $WorksheetName -AuditResults $AuditResults
} }
# Output the merged data if the user chooses to skip the update
if ($SkipUpdate) { if ($SkipUpdate) {
return $mergedData return $mergedData
} else { } else {
# Update the Excel worksheet with the merged data
Update-CISExcelWorksheet -ExcelPath $ExcelPath -WorksheetName $WorksheetName -Data $mergedData Update-CISExcelWorksheet -ExcelPath $ExcelPath -WorksheetName $WorksheetName -Data $mergedData
} }
} }
} }