PowerShell for Windows Admins

Sep 20 2013   1:51PM GMT

ErrorAction and WarningAction



Posted by: Richard Siddaway
Tags:
PowerShell Basics

You’ve all probably done something like this:

£> Get-Process -Name xyz
Get-Process : Cannot find a process with the name “xyz”. Verify the process name and call the cmdlet again.
At line:1 char:1
+ Get-Process -Name xyz
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (xyz:String) [Get-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand

First though of many people is to do this

Get-Process -Name xyz -ErrorAction silentlycontinue

Now, thats not good as you don’t know that an error has occurred. Its better to deal with it rather than suppress it so you wrap it in a try-catch:

£> try {
>> Get-Process -Name xyz -ErrorAction Stop
>> }
>> catch{
>> “oops”
>> }
>>
oops

In reality you would want to do more than say “oops”

There are a number of situations where cmdlets give you a warning rather than an error. if you want to suppress them use –WarningAction. It has the same options as –ErrorAction

SilentlyContinue. Suppresses the warning message and continues
executing the command.

Continue. Displays the warning message and continues executing
the command. “Continue” is the default value.

Inquire. Displays the warning message and prompts you for
confirmation before continuing execution. This value is rarely
used.

Stop. Displays the warning message and stops executing the
command.

More details in about_Common_Parameters

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: