I ran into a curious behavior today on how errors are written to the console. Given my example below, I would expect the $err.ErrorRecord to be written the same way regardless of whether write-host/warning/error is used.
function Run-Error() {
try {
$os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName "fake" -ErrorAction Stop -ErrorVariable err
} catch {
Write-Host "---Wrote to host---" -ForegroundColor Cyan
Write-Host $err.ErrorRecord
Write-Host "---Wrote to Warning---" -ForegroundColor Cyan
Write-Warning $err.ErrorRecord
Write-Host "---Wrote to error---" -ForegroundColor Cyan
Write-Error $err.ErrorRecord
}
}
Run-Error
However, I get the following output:
---Wrote to host---
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
---Wrote to Warning---
WARNING: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
---Wrote to error---
Run-Error : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\git\powershell-training\main.ps1:21 char:1
+ Run-Error
+ ~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Run-Error
Since I’m writing the same variable, I would expect the same results. I also found this strange because evaluating $err.ErrorRecord in the debugger produces output similar to Write-Error. However, if I do $err.ErrorRecord.ToString() in the debug console, I get ‘The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)’, consistent with Write-Host/Warning
Why are the outputs for these fomatted differently?