From b07344bb71b4e61d9bf00d5d5703cc822babda81 Mon Sep 17 00:00:00 2001 From: DrIOS <58635327+DrIOSX@users.noreply.github.com> Date: Mon, 17 Jun 2024 19:57:31 -0500 Subject: [PATCH] fix: Fixed merging and added date: --- source/Private/Merge-CISExcelAndCsvData.ps1 | 29 ++++++++++++++++++--- source/Private/New-MergedObject.ps1 | 2 ++ source/Private/Update-CISExcelWorksheet.ps1 | 10 +++++++ source/Private/Update-WorksheetCell.ps1 | 4 +-- source/Public/Sync-CISExcelAndCsvData.ps1 | 1 - 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/source/Private/Merge-CISExcelAndCsvData.ps1 b/source/Private/Merge-CISExcelAndCsvData.ps1 index 64fc4ef..b5381f1 100644 --- a/source/Private/Merge-CISExcelAndCsvData.ps1 +++ b/source/Private/Merge-CISExcelAndCsvData.ps1 @@ -16,23 +16,44 @@ 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 } - $mergedData = foreach ($item in $import) { + # Ensure headers are included in the merged data + $headers = @() + $firstItem = $import[0] + foreach ($property in $firstItem.PSObject.Properties) { + $headers += $property.Name + } + $headers += 'CSV_Connection', 'CSV_Status', 'CSV_Date', 'CSV_Details', 'CSV_FailureReason' + + $mergedData = @() + foreach ($item in $import) { $csvRow = $csvData | Where-Object { $_.Rec -eq $item.'recommendation #' } if ($csvRow) { - New-MergedObject -ExcelItem $item -CsvRow $csvRow + $mergedData += New-MergedObject -ExcelItem $item -CsvRow $csvRow } else { - $item + $mergedData += New-MergedObject -ExcelItem $item -CsvRow ([PSCustomObject]@{Connection=$null; Status=$null; Date=$null; Details=$null; FailureReason=$null}) } } - return $mergedData + # Create a new PSObject array with headers included + $result = @() + foreach ($item in $mergedData) { + $newItem = New-Object PSObject + foreach ($header in $headers) { + $newItem | Add-Member -MemberType NoteProperty -Name $header -Value $item.$header -Force + } + $result += $newItem + } + + return $result } } diff --git a/source/Private/New-MergedObject.ps1 b/source/Private/New-MergedObject.ps1 index e371982..aaf3c50 100644 --- a/source/Private/New-MergedObject.ps1 +++ b/source/Private/New-MergedObject.ps1 @@ -10,6 +10,7 @@ function New-MergedObject { ) $newObject = New-Object PSObject + $currentDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss" foreach ($property in $ExcelItem.PSObject.Properties) { $newObject | Add-Member -MemberType NoteProperty -Name $property.Name -Value $property.Value -Force @@ -17,6 +18,7 @@ function New-MergedObject { $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_Date' -Value $currentDate -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 diff --git a/source/Private/Update-CISExcelWorksheet.ps1 b/source/Private/Update-CISExcelWorksheet.ps1 index d94a8fe..a7ad633 100644 --- a/source/Private/Update-CISExcelWorksheet.ps1 +++ b/source/Private/Update-CISExcelWorksheet.ps1 @@ -24,6 +24,16 @@ function Update-CISExcelWorksheet { throw "Worksheet '$WorksheetName' not found in '$ExcelPath'" } + # Ensure headers are set + $firstItem = $Data[0] + $colIndex = 1 + foreach ($property in $firstItem.PSObject.Properties) { + if ($worksheet.Cells[1, $colIndex].Value -eq $null -or $worksheet.Cells[1, $colIndex].Value -ne $property.Name) { + $worksheet.Cells[1, $colIndex].Value = $property.Name + } + $colIndex++ + } + # Update the worksheet with the provided data Update-WorksheetCell -Worksheet $worksheet -Data $Data -StartingRowIndex $StartingRowIndex diff --git a/source/Private/Update-WorksheetCell.ps1 b/source/Private/Update-WorksheetCell.ps1 index da4fb61..d92f044 100644 --- a/source/Private/Update-WorksheetCell.ps1 +++ b/source/Private/Update-WorksheetCell.ps1 @@ -10,8 +10,8 @@ function Update-WorksheetCell { $firstItem = $Data[0] $colIndex = 1 foreach ($property in $firstItem.PSObject.Properties) { - # Update headers if they don't exist or if explicitly needed - if ($Worksheet.Cells[1, $colIndex].Value -ne $property.Name) { + if ($StartingRowIndex -eq 2 -and $Worksheet.Cells[1, $colIndex].Value -eq $null) { + # Add header if it's not present $Worksheet.Cells[1, $colIndex].Value = $property.Name } $colIndex++ diff --git a/source/Public/Sync-CISExcelAndCsvData.ps1 b/source/Public/Sync-CISExcelAndCsvData.ps1 index df38696..7e9a00e 100644 --- a/source/Public/Sync-CISExcelAndCsvData.ps1 +++ b/source/Public/Sync-CISExcelAndCsvData.ps1 @@ -84,4 +84,3 @@ function Sync-CISExcelAndCsvData { } } } -