fix: Fixed merging and added date:
This commit is contained in:
@@ -16,23 +16,44 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
$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 #' }
|
$csvRow = $csvData | Where-Object { $_.Rec -eq $item.'recommendation #' }
|
||||||
if ($csvRow) {
|
if ($csvRow) {
|
||||||
New-MergedObject -ExcelItem $item -CsvRow $csvRow
|
$mergedData += New-MergedObject -ExcelItem $item -CsvRow $csvRow
|
||||||
} else {
|
} 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -10,6 +10,7 @@ function New-MergedObject {
|
|||||||
)
|
)
|
||||||
|
|
||||||
$newObject = New-Object PSObject
|
$newObject = New-Object PSObject
|
||||||
|
$currentDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||||
|
|
||||||
foreach ($property in $ExcelItem.PSObject.Properties) {
|
foreach ($property in $ExcelItem.PSObject.Properties) {
|
||||||
$newObject | Add-Member -MemberType NoteProperty -Name $property.Name -Value $property.Value -Force
|
$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_Connection' -Value $CsvRow.Connection -Force
|
||||||
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_Status' -Value $CsvRow.Status -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_Details' -Value $CsvRow.Details -Force
|
||||||
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_FailureReason' -Value $CsvRow.FailureReason -Force
|
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_FailureReason' -Value $CsvRow.FailureReason -Force
|
||||||
|
|
||||||
|
@@ -24,6 +24,16 @@ function Update-CISExcelWorksheet {
|
|||||||
throw "Worksheet '$WorksheetName' not found in '$ExcelPath'"
|
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 the worksheet with the provided data
|
||||||
Update-WorksheetCell -Worksheet $worksheet -Data $Data -StartingRowIndex $StartingRowIndex
|
Update-WorksheetCell -Worksheet $worksheet -Data $Data -StartingRowIndex $StartingRowIndex
|
||||||
|
|
||||||
|
@@ -10,8 +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) {
|
||||||
# Update headers if they don't exist or if explicitly needed
|
if ($StartingRowIndex -eq 2 -and $Worksheet.Cells[1, $colIndex].Value -eq $null) {
|
||||||
if ($Worksheet.Cells[1, $colIndex].Value -ne $property.Name) {
|
# Add header if it's not present
|
||||||
$Worksheet.Cells[1, $colIndex].Value = $property.Name
|
$Worksheet.Cells[1, $colIndex].Value = $property.Name
|
||||||
}
|
}
|
||||||
$colIndex++
|
$colIndex++
|
||||||
|
@@ -84,4 +84,3 @@ function Sync-CISExcelAndCsvData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user