fix: Refactor sync function to one simple function
This commit is contained in:
@@ -1,64 +0,0 @@
|
||||
function Merge-CISExcelAndCsvData {
|
||||
[CmdletBinding(DefaultParameterSetName = 'CsvInput')]
|
||||
[OutputType([PSCustomObject[]])]
|
||||
param (
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$ExcelPath,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$WorksheetName,
|
||||
|
||||
[Parameter(Mandatory = $true, ParameterSetName = 'CsvInput')]
|
||||
[string]$CsvPath,
|
||||
|
||||
[Parameter(Mandatory = $true, ParameterSetName = 'ObjectInput')]
|
||||
[CISAuditResult[]]$AuditResults
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
# Extract recommendation numbers from the CSV
|
||||
$csvRecs = $csvData | Select-Object -ExpandProperty Rec
|
||||
|
||||
# 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) {
|
||||
# Check if the recommendation number exists in the CSV
|
||||
$recNum = $item.'recommendation #'
|
||||
if ($csvRecs -contains $recNum) {
|
||||
$csvRow = $csvData | Where-Object { $_.Rec -eq $recNum }
|
||||
$mergedData += New-MergedObject -ExcelItem $item -CsvRow $csvRow
|
||||
} else {
|
||||
$mergedData += $item
|
||||
}
|
||||
}
|
||||
|
||||
# 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
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
function New-MergedObject {
|
||||
[CmdletBinding()]
|
||||
[OutputType([PSCustomObject])]
|
||||
param (
|
||||
[Parameter(Mandatory = $true)]
|
||||
[psobject]$ExcelItem,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[psobject]$CsvRow
|
||||
)
|
||||
|
||||
$newObject = New-Object PSObject
|
||||
$currentDate = Get-Date -Format "yyyy-MM-ddTHH:mm:ss"
|
||||
|
||||
foreach ($property in $ExcelItem.PSObject.Properties) {
|
||||
$newObject | Add-Member -MemberType NoteProperty -Name $property.Name -Value $property.Value -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
|
||||
if ($CsvRow.Status -ne $null) {
|
||||
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_Date' -Value $currentDate -Force
|
||||
} else {
|
||||
$newObject | Add-Member -MemberType NoteProperty -Name 'CSV_Date' -Value $null -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
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
function Update-CISExcelWorksheet {
|
||||
[OutputType([void])]
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$ExcelPath,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$WorksheetName,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[psobject[]]$Data,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[int]$StartingRowIndex = 2 # Default starting row index, assuming row 1 has headers
|
||||
)
|
||||
|
||||
process {
|
||||
# Load the existing Excel sheet
|
||||
$excelPackage = Open-ExcelPackage -Path $ExcelPath
|
||||
$worksheet = $excelPackage.Workbook.Worksheets[$WorksheetName]
|
||||
|
||||
if (-not $worksheet) {
|
||||
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
|
||||
$validRows = $Data | Where-Object { $_.'recommendation #' -ne $null }
|
||||
Update-WorksheetCell -Worksheet $worksheet -Data $validRows -StartingRowIndex $StartingRowIndex
|
||||
|
||||
# Save and close the Excel package
|
||||
Close-ExcelPackage $excelPackage
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
function Update-WorksheetCell {
|
||||
[OutputType([void])]
|
||||
param (
|
||||
$Worksheet,
|
||||
$Data,
|
||||
$StartingRowIndex
|
||||
)
|
||||
|
||||
# Check and set headers
|
||||
$firstItem = $Data[0]
|
||||
$colIndex = 1
|
||||
foreach ($property in $firstItem.PSObject.Properties) {
|
||||
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++
|
||||
}
|
||||
|
||||
# Iterate over each row in the data and update cells
|
||||
$rowIndex = $StartingRowIndex
|
||||
foreach ($item in $Data) {
|
||||
$colIndex = 1
|
||||
foreach ($property in $item.PSObject.Properties) {
|
||||
$Worksheet.Cells[$rowIndex, $colIndex].Value = $property.Value
|
||||
$colIndex++
|
||||
}
|
||||
$rowIndex++
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user