diff --git a/source/Private/Merge-CISExcelAndCsvData.ps1 b/source/Private/Merge-CISExcelAndCsvData.ps1 index 7d2f039..64fc4ef 100644 --- a/source/Private/Merge-CISExcelAndCsvData.ps1 +++ b/source/Private/Merge-CISExcelAndCsvData.ps1 @@ -16,27 +16,23 @@ function Merge-CISExcelAndCsvData { ) process { - # Import data from Excel $import = Import-Excel -Path $ExcelPath -WorksheetName $WorksheetName - # Import data from CSV or use provided object $csvData = if ($PSCmdlet.ParameterSetName -eq 'CsvInput') { Import-Csv -Path $CsvPath } else { $AuditResults } - # Iterate over each item in the imported Excel object and merge with CSV data or audit results $mergedData = foreach ($item in $import) { $csvRow = $csvData | Where-Object { $_.Rec -eq $item.'recommendation #' } if ($csvRow) { New-MergedObject -ExcelItem $item -CsvRow $csvRow } else { - New-MergedObject -ExcelItem $item -CsvRow ([PSCustomObject]@{Connection=$null;Status=$null; Details=$null; FailureReason=$null }) + $item } } - # Return the merged data return $mergedData } -} \ No newline at end of file +} diff --git a/source/Private/New-MergedObject.ps1 b/source/Private/New-MergedObject.ps1 index 50f7497..e371982 100644 --- a/source/Private/New-MergedObject.ps1 +++ b/source/Private/New-MergedObject.ps1 @@ -12,11 +12,13 @@ function New-MergedObject { $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 $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_Details' -Value $CsvRow.Details - $newObject | Add-Member -MemberType NoteProperty -Name 'CSV_FailureReason' -Value $CsvRow.FailureReason + + $newObject | Add-Member -MemberType NoteProperty -Name 'CSV_Connection' -Value $CsvRow.Connection -Force + $newObject | Add-Member -MemberType NoteProperty -Name 'CSV_Status' -Value $CsvRow.Status -Force + $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 } diff --git a/source/Private/Update-CISExcelWorksheet.ps1 b/source/Private/Update-CISExcelWorksheet.ps1 index 9976f76..d94a8fe 100644 --- a/source/Private/Update-CISExcelWorksheet.ps1 +++ b/source/Private/Update-CISExcelWorksheet.ps1 @@ -24,11 +24,10 @@ function Update-CISExcelWorksheet { throw "Worksheet '$WorksheetName' not found in '$ExcelPath'" } - # Update the worksheet with the provided data Update-WorksheetCell -Worksheet $worksheet -Data $Data -StartingRowIndex $StartingRowIndex # Save and close the Excel package Close-ExcelPackage $excelPackage } -} \ No newline at end of file +} diff --git a/source/Private/Update-WorksheetCell.ps1 b/source/Private/Update-WorksheetCell.ps1 index 9708c1c..da4fb61 100644 --- a/source/Private/Update-WorksheetCell.ps1 +++ b/source/Private/Update-WorksheetCell.ps1 @@ -10,7 +10,8 @@ function Update-WorksheetCell { $firstItem = $Data[0] $colIndex = 1 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 } $colIndex++ diff --git a/source/Public/Sync-CISExcelAndCsvData.ps1 b/source/Public/Sync-CISExcelAndCsvData.ps1 index 8e91b95..df38696 100644 --- a/source/Public/Sync-CISExcelAndCsvData.ps1 +++ b/source/Public/Sync-CISExcelAndCsvData.ps1 @@ -66,25 +66,22 @@ function Sync-CISExcelAndCsvData { ) process { - # Verify ImportExcel module is available $requiredModules = Get-RequiredModule -SyncFunction foreach ($module in $requiredModules) { Assert-ModuleAvailability -ModuleName $module.ModuleName -RequiredVersion $module.RequiredVersion -SubModuleName $module.SubModuleName } - # Merge Excel and CSV data or Audit Results if ($PSCmdlet.ParameterSetName -eq 'CsvInput') { $mergedData = Merge-CISExcelAndCsvData -ExcelPath $ExcelPath -WorksheetName $WorksheetName -CsvPath $CsvPath } else { $mergedData = Merge-CISExcelAndCsvData -ExcelPath $ExcelPath -WorksheetName $WorksheetName -AuditResults $AuditResults } - # Output the merged data if the user chooses to skip the update if ($SkipUpdate) { return $mergedData } else { - # Update the Excel worksheet with the merged data Update-CISExcelWorksheet -ExcelPath $ExcelPath -WorksheetName $WorksheetName -Data $mergedData } } } +