1

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?

2 Answers 2

1

Perhaps this is what you are after?

# set up a hash with possible Ping status codes
$status = @{
    11001 = 'Buffer Too Small'
    11002 = 'Destination Net Unreachable'
    11003 = 'Destination Host Unreachable'
    11004 = 'Destination Protocol Unreachable'
    11005 = 'Destination Port Unreachable'
    11006 = 'No Resources'
    11007 = 'Bad Option'
    11008 = 'Hardware Error'
    11009 = 'Packet Too Big'
    11010 = 'Request Timed Out'
    11011 = 'Bad Request'
    11012 = 'Bad Route'
    11013 = 'TimeToLive Expired Transit'
    11014 = 'TimeToLive Expired Reassembly'
    11015 = 'Parameter Problem'
    11016 = 'Source Quench'
    11017 = 'Option Too Big'
    11018 = 'Bad Destination'
    11032 = 'Negotiating IPSEC'
    11050 = 'General Failure'
}

$server = '165.100.10.10'
$ping   = (Get-WmiObject -Class Win32_PingStatus -Filter "Address='$server'").StatusCode

if (!$ping -or [int]$ping -ne 0) {
    $err = if ( $status[[int]$ping]) { $status[[int]$ping] } else { "Unknown Failure" }
    $output = "FAILED; $ping; $err"

}
else { $output = "PASSED" }

Write-Host $output

The above example outputs:

FAILED; 11010; Request Timed Out
Sign up to request clarification or add additional context in comments.

2 Comments

Many thanks. As a beginner in Powershell, I do not really understand the line of code that you are assigning to $ping. Do you mind further explaining your code or referring me to other sources that can help me understand? I am also trying to get the same kind of script that returns me an error code and message but with other commands such as Get-NetAdapter.
@Komputer Get-WmiObject -Class Win32_PingStatus -Filter "Address='$server'" returns an object with multiple properties after performing a ping to the specified server name or IP address. The brackets around the command 'capture' this object and since we are only interested in the property StatusCode, we can simply use the 'dot' syntax to get that single property. It is in fact a shorthand code for Get-WmiObject -Class Win32_PingStatus -Filter "Address='lambiek'" | Select-Object -ExpandProperty StatusCode. To see the properties and methods of an object, pipe it through to Get-Member.
0

There are multiple ways to handle errors and verifying the. First and foremost, I would like you to keep the main code under a try/catch block and capture the error message so that you get to know what exactly the error is. Below is the modification of your code.

param([String]$Cab_Type)
$output
try
{
    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>"
}
catch
{
    $_.Exception.Message
}

Also, go through about 1. TRY CATCH FINALLY in PS

Apart from that you can use $Error variable to get to know about all the errors.

Go through 2. About $Error variable and how to use it effectively

Also you need to understand 3. Powershell Exceptions and Everything you ever wanted to know

Hope it helps and gives you a direction.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.