Prof. Powershell

To Err[or] is Human

And to use the -ErrorVariable cmdlet to keep track of them is divine.

Yet another among a string of common parameters for most cmdlets is -ErrorVariable. Whenever an error occurs with a cmdlet, it is automatically written to the $error variable. This is a collection of the most recent errors in your PowerShell session. You can access the most recent error like this:

PS C:\> $error[0]

But there may be times when you want to save the error information within a command. Perhaps you want to log it to a text file or perform some fancy error trapping. When you use -ErrorVariable, any errors that occur when running the cmdlet are written to the variable you specify as well as $error:

PS C:\> get-process foobar -errorvariable myerror
Get-Process : Cannot find a process with the name 'foobar'. Verify the process name and call the cmdlet again.
At line:1 char:12
+ get-process <<<< foobar -errorvariable myerror
PS C:\> $myerror
Get-Process : Cannot find a process with the name 'foobar'. Verify the process name and call the cmdlet again.
At line:1 char:12
+ get-process <<<< foobar -errorvariable myerror
PS C:\> $error[0]
Get-Process : Cannot find a process with the name 'foobar'. Verify the process name and call the cmdlet again.
At line:1 char:12
+ get-process <<<< foobar -errorvariable myerror
PS C:\>

In this example I attempted to get a non-existent process. I specified a variable for -ErrorVariable called myerror. As you can see, it is identical to the last error written to $error. But there is one big advantage. If I run another PowerShell command that generates an error, my Get-Process error moves down in order. After enough errors, it will be gone altogether.

However, if I need to refer back to the error I can still access it in $myerror. This variable is also an array like $error, although in this case it only has one entry which is why I can get by with simply invoking $myerror. To access any properties directly, I need to use an index number:

PS C:\> $myerror[0].categoryinfo

Category : ObjectNotFound
Activity : Get-Process
Reason : ProcessCommandException
TargetName : foobar
TargetType : String

Although it might be more thorough to treat $myerror as the array, it is in case the cmdlet raised more than one error:

PS C:\> $myerror | foreach {
>> write ("Exception: {0}" -f $_.Exception)
>> write ("Category: {0}" -f $_.CategoryInfo.Category)
>> write ("Command: {0}" -f $_.InvocationInfo.MyCommand)
>> }
>>
Exception: Microsoft.PowerShell.Commands.ProcessCommandException: Cannot find a process with the name 'foobar'. Verify
the process name and call the cmdlet again.
Category: ObjectNotFound
Command: Get-Process
PS C:\>

You can just as easily write this to a text file using Out-File, Set-Content or Add-Content. My mom always said we learn from our mistakes. Use -errorVariable to keep track of yours.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular