Skip to content

Commit

Permalink
Insert FYI when truncating test result data. Add info output to CSV a…
Browse files Browse the repository at this point in the history
…nd XLSX export.
  • Loading branch information
SamErde committed Feb 3, 2025
1 parent 8165546 commit 3143f99
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion powershell/public/Convert-MtJsonResultsToFlatObject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,28 @@
'you’re' = 'you are'
'aren’nt' = 'are not'
} ; [void]$ReplacementStrings # Not Used Yet

[string]$TruncationFYI = 'NOTE: DETAILS ARE TRUNCATED DUE TO FIELD SIZE LIMITATIONS. PLEASE SEE THE HTML REPORT FOR FULL DETAILS.'
#endregion ReplacementStrings

$MaesterResults = New-Object System.Collections.Generic.List[PSObject]

$JsonData = (Get-Content -Path $JsonFilePath | ConvertFrom-Json).Tests
$JsonData | ForEach-Object {
# Truncate the ResultDetail.TestResult data if it is longer than 30000 characters.
if ($_.ResultDetail.TestResult.Length -gt 30000) {
$TestResultDetail = "$TruncationFYI`n`n$($TestResultDetail -replace '(?s)(.*?)#### Impacted resources.*?#### Remediation actions:','$1#### Remediation actions:')"
} else {
$TestResultDetail = $_.ResultDetail.TestResult
}

$MaesterResults.Add([PSCustomObject]@{
Name = $_.Name
Tag = $_.Tag -join ', '
Block = $_.Block
Result = $_.Result
Description = $_.ResultDetail.TestDescription
ResultDetail = "$($_.ResultDetail.TestResult -replace '(?s)(.*?)#### Impacted resources.*?#### Remediation actions:','$1#### Remediation actions:')"
ResultDetail = $TestResultDetail
TestSkipped = $_.ResultDetail.TestSkipped
SkippedReason = $_.ResultDetail.SkippedReason
ErrorMessage = $_.ErrorRecord.Exception.Message
Expand All @@ -99,13 +108,15 @@

try {
$MaesterResults | Export-Csv -Path $CsvFilePath -UseQuotes Always -NoTypeInformation
Write-Information "Exported the Maester test results to '$CsvFilePath'." -InformationAction Continue
} catch {
Write-Error "Failed to export the Maester test results to a CSV file. $_"
}

if ($ExportExcel.IsPresent) {
try {
$MaesterResults | Export-Excel -Path $ExcelFilePath -FreezeTopRow -AutoFilter -BoldTopRow -WorksheetName 'Results'
Write-Information "Exported the Maester test results to '$ExcelFilePath'." -InformationAction Continue
} catch [System.Management.Automation.CommandNotFoundException] {
Write-Error "The ImportExcel module is required to export the Maester test results to an Excel file. Install the module using ``Import-Module -Name 'ImportExcel'`` and try again."

Expand All @@ -117,4 +128,5 @@
if ($Passthru.IsPresent) {
$MaesterResults
}

} #end function Convert-MtJsonResultsToFlatObject

0 comments on commit 3143f99

Please sign in to comment.