Skip to content

Commit

Permalink
Add Try Catch for write to screen
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaulson45 committed Jun 13, 2024
1 parent c39731b commit 3ce2383
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 33 deletions.
25 changes: 17 additions & 8 deletions Diagnostics/HealthChecker/Writers/Write-Functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,23 @@ function Write-HostLog ($message) {

function Write-OutColumns($OutColumns) {
if ($null -ne $OutColumns) {
$stringOutput = $null
$OutColumns.DisplayObject |
Out-Columns -Properties $OutColumns.SelectProperties `
-ColorizerFunctions $OutColumns.ColorizerFunctions `
-IndentSpaces $OutColumns.IndentSpaces `
-StringOutput ([ref]$stringOutput)
$stringOutput | Out-File ($Script:OutputFullPath) -Append
Write-DebugLog $stringOutput
try {
$stringOutput = $null
$params = @{
Properties = $OutColumns.SelectProperties
ColorizerFunctions = $OutColumns.ColorizerFunctions
IndentSpaces = $OutColumns.IndentSpaces
StringOutput = ([ref]$stringOutput)
}
$OutColumns.DisplayObject | Out-Columns @params
$stringOutput | Out-File ($Script:OutputFullPath) -Append
Write-DebugLog $stringOutput
} catch {
# We do not want to call Invoke-CatchActions here because we want the issues reported.
Write-Verbose "Failed to export Out-Columns. Inner Exception: $_"
$s = $OutColumns.DisplayObject | Out-String
Write-DebugLog $s
}
}
}

Expand Down
62 changes: 37 additions & 25 deletions Diagnostics/HealthChecker/Writers/Write-ResultsToScreen.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,48 @@ function Write-ResultsToScreen {
Write-Verbose "Working on Key Group: $($keyGrouping.Name)"
Write-Verbose "Total lines to write: $($ResultsToWrite[$keyGrouping].Count)"

if ($keyGrouping.DisplayGroupName) {
Write-Grey($keyGrouping.Name)
$dashes = [string]::empty
1..($keyGrouping.Name.Length) | ForEach-Object { $dashes = $dashes + "-" }
Write-Grey($dashes)
}
try {
if ($keyGrouping.DisplayGroupName) {
Write-Grey($keyGrouping.Name)
$dashes = [string]::empty
1..($keyGrouping.Name.Length) | ForEach-Object { $dashes = $dashes + "-" }
Write-Grey($dashes)
}

foreach ($line in $ResultsToWrite[$keyGrouping]) {
$tab = [string]::Empty
foreach ($line in $ResultsToWrite[$keyGrouping]) {
try {
$tab = [string]::Empty

if ($line.TabNumber -ne 0) {
1..($line.TabNumber) | ForEach-Object { $tab = $tab + "`t" }
}
if ($line.TabNumber -ne 0) {
1..($line.TabNumber) | ForEach-Object { $tab = $tab + "`t" }
}

if ([string]::IsNullOrEmpty($line.Name)) {
$displayLine = $line.DisplayValue
} else {
$displayLine = [string]::Concat($line.Name, ": ", $line.DisplayValue)
}
if ([string]::IsNullOrEmpty($line.Name)) {
$displayLine = $line.DisplayValue
} else {
$displayLine = [string]::Concat($line.Name, ": ", $line.DisplayValue)
}

$writeValue = "{0}{1}" -f $tab, $displayLine
switch ($line.WriteType) {
"Grey" { Write-Grey($writeValue) }
"Yellow" { Write-Yellow($writeValue) }
"Green" { Write-Green($writeValue) }
"Red" { Write-Red($writeValue) }
"OutColumns" { Write-OutColumns($line.OutColumns) }
$writeValue = "{0}{1}" -f $tab, $displayLine
switch ($line.WriteType) {
"Grey" { Write-Grey($writeValue) }
"Yellow" { Write-Yellow($writeValue) }
"Green" { Write-Green($writeValue) }
"Red" { Write-Red($writeValue) }
"OutColumns" { Write-OutColumns($line.OutColumns) }
}
} catch {
# We do not want to call Invoke-CatchActions here because we want the issues reported.
Write-Verbose "Failed inside the section loop writing. Writing out a blank line and continuing. Inner Exception: $_"
Write-Grey ""
}
}
}

Write-Grey("")
Write-Grey ""
} catch {
# We do not want to call Invoke-CatchActions here because we want the issues reported.
Write-Verbose "Failed in $($MyInvocation.MyCommand) outside section writing loop. Inner Exception: $_"
Write-Grey ""
}
}
}

0 comments on commit 3ce2383

Please sign in to comment.