I am trying to run a Powershell script and display the error code and error message if it fails. It is supposed to output me a result in this format:
"FAILED;ErrorCode;ErrorMessage;"
Here is my script:
param([String]$Cab_Type)
$output
if(!(Test-Connection -Cn 165.100.10.10 -BufferSize 16 -Count 1 -quiet))
{
$output = "FAILED; " + $LASTEXITCODE + ";" + $error[0] + ";"
}
else
{
$output = "PASSED"
}
Write-Host $Cab_Type
Write-Host "<ScriptResult_Start>"
Write-Host $output
Write-Host "<ScriptResult_End>"
I am trying to intentionally ping an address that I know will fail. When running the script, it returns me the error message but not the error code.
Does $LASTEXITCODE not return the error code of the script? Even if my script worked, does it only return 0 or 1? Is there any way of getting the actual error code of the script?