In this post, I will talk about two powershell built-in variables that tells if last command/script executed successfully or failed.
As you start writing more sophisticated scripts using Powershell, it is important to perform proper error handling in the scripts. There are several mechanisms available in PS to perform error handling. In this post, I will talk about basic methods which you can use either in scripts or from command lines easily.
Powershell has two built-in variables $LASTEXITCODE and $? which can tell if the last executed script/code/win32 executable completed successfully or in error
$LASTEXITCODE:
This is equivalent to %errorlevel% variable in cmd shell. When you execute a command in cmd.exe the execution status is stored in %errorlevel% variable which can be used in batch scripting to determine the execution status of previous command. Similarly in Powershell, when a win32 executable is executed, the return code is stored in $LASTEXITCODE variable. Generally a zero(0) value in $LASTEXITCODE is treated as success and any other non-zero are treated as failures. You can also choose to perform actions based on the return code of the executable.
Example:
PS C:\scripts>$LASTEXITCODE=0
PS C:\scripts> sc.exe query spooler1
[SC] EnumQueryServicesStatus:OpenService FAILED 1060:
The specified service does not exist as an installed service.
PS C:\scripts> $LASTEXITCODE
1060
PS C:\scripts> sc.exe query spooler
SERVICE_NAME: spooler
TYPE : 110 WIN32_OWN_PROCESS (interactive)
STATE : 4 RUNNING
(STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
PS C:\scripts> $LASTEXITCODE
0
PS C:\scripts>
In above examples, when I queried for spooler1 service(non-existing one) the value of $LASTEXITCODE is changed to 1060, the return code from sc.exe
The $? Variable:
This is another built-in variable in Powershell which has the same functionality of $LASTEXITCODE except that it stores the execution status of last powershell command AND win32 applications execution status. You can use this in Powershell scripts or while executing one-liners from powershell console. This variable won’t tell the return code, but it tells you whether the last PS command or Win32 executable executed successfully or not. That means, it will contain either of two values, $TRUE or $FALSE. When a command is executed successfully, this variable value is changed to $TRUE. Similarly, it is changed to $FALSE when the last PS command is failed. See below examples for more clarity
PS C:\scripts> Get-Service -Name adfasdf
Get-Service : Cannot find any service with service name ‘adfasdf’.
At line:1 char:12
+ Get-Service <<<< -Name adfasdf
+ CategoryInfo : ObjectNotFound: (adfasdf:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
PS C:\scripts> Get-Service -Name adfasdf
Get-Service : Cannot find any service with service name ‘adfasdf’.
At line:1 char:12
+ Get-Service <<<< -Name adfasdf
+ CategoryInfo : ObjectNotFound: (adfasdf:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
PS C:\scripts> $?
False
PS C:\scripts>
You can also use this variable inside your script to know the status of previous executed PS command status. See below script for usage criteria.
Get-Service -Name Spooler1 if($?) { Write-Host "The last PS command executed successfully" } else { write-host "The last PS command failed" } Get-Service -Name Spooler if($?) { Write-Host "The last PS command executed successfully" } else { write-host "The last PS command failed" }
Hope this helps…
Comments on this entry are closed.
Thank you,
Very nice detailed post.