Error handling does not necessarily have to be complex. It can be as simple as checking whether the last command executed successfully:

# suppress errors by default
$ErrorActionPreference = 'SilentlyContinue'
# if a command runs into an error...
Get-Process -Name zumsel
# ...then $? is $false, and you can exit PowerShell
# with a return value, i.e. 55
if (!$?) { exit 55 }

PowerShell reports back in $? whether the last command encountered an error and returns $false in such as case. Using “exit” and a positive number, you could then bail out, exit your script, and return an exit code to the caller.

Twitter This Tip! ReTweet this Tip!

Anonymous
  • OK, never mind. This part of the forum, it like chat. Everything is on one line. Sorry.
  • So, I am not sure of the confusion. Maybe it's just the way that sentance is written. However, let's break down what the author is presenting Error handling does not necessarily have to be complex. It can be as simple as checking whether the last command executed successfully: suppress errors by default $ErrorActionPreference = 'SilentlyContinue' if a command runs into an error... (though it won't be shown on the screen due to the $ErrorActionPreference setting) Get-Process -Name zumsel There is no process called zumsel - so it errors out Get-Process : Cannot find a process with the name "zumsel". Verify the process name and call the cmdlet again. At line:1 char:1 Get-Process -Name zumsel ... ...ObjectNotFound: (zumsel:String) [Get-Process], ProcessCommandException ... ...then $? is $false, and you can exit PowerShell with a return value, i.e. 55 or whatever error code integer you wish if (!$?) { exit 55 } PowerShell reports back in $? whether the last command encountered an error and returns $false in such as case. $? contains the result of False or True depending on error state of failure or success of a command/script/function $ErrorActionPreference = 'SilentlyContinue' Get-Process -Name notepad $? if (!$?) { exit 55 } Else {'Your process is running and here are the details'} There is a process call notepad, so output the resutls and error state ...ProcessName ...----------- ...notepad True Your process is running and here are the details Using “exit” and a positive number, you could then bail out, exit your script, and return an exit code to the caller. HTH
  • Well, that first post back ended up a jumbled mess, not real sure why, but let's try again. Hoping for the forum not destroying my post again. 8^}
  • duncfair... So, I am not sure of the confusion. Maybe it's just the way that sentance is written. However, let's break down what the author is presenting Error handling does not necessarily have to be complex. It can be as simple as checking whether the last command executed successfully: # suppress errors by default $ErrorActionPreference = 'SilentlyContinue' # if a command runs into an error... # (though it won't be shown on the screen due to the $ErrorActionPreference setting) Get-Process -Name zumsel There is no process called zumsel - so it errors out Get-Process : Cannot find a process with the name "zumsel". Verify the process name and call the cmdlet again. At line:1 char:1 + Get-Process -Name zumsel + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (zumsel:String) [Get-Process], ProcessCommandException + FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand # ...then $? is $false, and you can exit PowerShell with a return value, i.e. 55 or whatever error code integer you wish if (!$?) { exit 55 } PowerShell reports back in $? whether the last command encountered an error and returns $false in such as case. $? contains the result of False or True depending on error state of failure or success of a command/script/function $ErrorActionPreference = 'SilentlyContinue' Get-Process -Name notepad $? if (!$?) { exit 55 } Else {'Your process is running and here are the details'} If there is a process call notepad, output the results and error state Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 551 31 12160 47816 6.19 11044 1 notepad True Your process is running and here are the details #> Using “exit” and a positive number, you could then bail out, exit your script, and return an exit code to the caller. HTH
  • There are only a few lines of explanation here and this one - "PowerShell reports back in $? whether the last command encountered an error and returns $false in such as case. " doesn't make sense. Please redo this tip.