add: Export to excel option for table exports

This commit is contained in:
DrIOS
2024-06-20 13:12:01 -05:00
parent 359d2890f8
commit ab0ef53bbd

View File

@@ -16,6 +16,8 @@
The path where the CSV files will be exported. The path where the CSV files will be exported.
.PARAMETER ExportOriginalTests .PARAMETER ExportOriginalTests
Switch to export the original audit results to a CSV file. Switch to export the original audit results to a CSV file.
.PARAMETER ExportToExcel
Switch to export the results to an Excel file.
.INPUTS .INPUTS
[CISAuditResult[]], [string] [CISAuditResult[]], [string]
.OUTPUTS .OUTPUTS
@@ -69,9 +71,15 @@ function Export-M365SecurityAuditTable {
[Parameter(Mandatory = $false, ParameterSetName = "ExportAllResultsFromAuditResults")] [Parameter(Mandatory = $false, ParameterSetName = "ExportAllResultsFromAuditResults")]
[Parameter(Mandatory = $false, ParameterSetName = "ExportAllResultsFromCsv")] [Parameter(Mandatory = $false, ParameterSetName = "ExportAllResultsFromCsv")]
[switch]$ExportOriginalTests [switch]$ExportOriginalTests,
)
[Parameter(Mandatory = $false, ParameterSetName = "ExportAllResultsFromAuditResults")]
[Parameter(Mandatory = $false, ParameterSetName = "ExportAllResultsFromCsv")]
[switch]$ExportToExcel
)
if ($ExportToExcel) {
Assert-ModuleAvailability -ModuleName ImportExcel -RequiredVersion "7.8.9"
}
if ($PSCmdlet.ParameterSetName -like "ExportAllResultsFromCsv" -or $PSCmdlet.ParameterSetName -eq "OutputObjectFromCsvSingle") { if ($PSCmdlet.ParameterSetName -like "ExportAllResultsFromCsv" -or $PSCmdlet.ParameterSetName -eq "OutputObjectFromCsvSingle") {
$AuditResults = Import-Csv -Path $CsvPath | ForEach-Object { $AuditResults = Import-Csv -Path $CsvPath | ForEach-Object {
$params = @{ $params = @{
@@ -166,7 +174,13 @@ function Export-M365SecurityAuditTable {
} }
else { else {
if (($result.Details -ne "No M365 E3 licenses found.") -and ($result.Details -ne "No M365 E5 licenses found.")) { if (($result.Details -ne "No M365 E3 licenses found.") -and ($result.Details -ne "No M365 E5 licenses found.")) {
if ($ExportToExcel) {
$xlsxPath = [System.IO.Path]::ChangeExtension($fileName, '.xlsx')
$result.Details | Export-Excel -Path $xlsxPath -WorksheetName Table -TableName Table -AutoSize -TableStyle Medium2
}
else {
$result.Details | Export-Csv -Path $fileName -NoTypeInformation $result.Details | Export-Csv -Path $fileName -NoTypeInformation
}
$exportedTests += $result.TestNumber $exportedTests += $result.TestNumber
} }
} }
@@ -191,9 +205,15 @@ function Export-M365SecurityAuditTable {
# Check for large details and update the AuditResults array # Check for large details and update the AuditResults array
$updatedAuditResults = Get-ExceededLengthResultDetail -AuditResults $AuditResults -TestNumbersToCheck $TestNumbersToCheck -ExportedTests $exportedTests -DetailsLengthLimit 30000 -PreviewLineCount 25 $updatedAuditResults = Get-ExceededLengthResultDetail -AuditResults $AuditResults -TestNumbersToCheck $TestNumbersToCheck -ExportedTests $exportedTests -DetailsLengthLimit 30000 -PreviewLineCount 25
$originalFileName = "$ExportPath\$timestamp`_M365FoundationsAudit.csv" $originalFileName = "$ExportPath\$timestamp`_M365FoundationsAudit.csv"
if ($ExportToExcel) {
$xlsxPath = [System.IO.Path]::ChangeExtension($originalFileName, '.xlsx')
$updatedAuditResults | Export-Excel -Path $xlsxPath -WorksheetName Table -TableName Table -AutoSize -TableStyle Medium2
}
else {
$updatedAuditResults | Export-Csv -Path $originalFileName -NoTypeInformation $updatedAuditResults | Export-Csv -Path $originalFileName -NoTypeInformation
} }
} }
}
elseif ($OutputTestNumber) { elseif ($OutputTestNumber) {
if ($results[0].Details) { if ($results[0].Details) {
return $results[0].Details return $results[0].Details