TRYing to CATCH Exceptions in Powershell... Meh!!!
Grumpy admin, was looking at a friend’s PowerShell script again – for some strange reason, my friends (yes I do have them honest) always want to send me their work for peer review and comments. It’s a compliment I guess, and I enjoy it. Code reviewing someone else’s code is a great insight in to their mind-set! Grumpy Admin really doesn’t mind, it just makes me slightly grumpy that their code is normally better than mine.
Something that I did notice, my friend’s code was doing a lot! Stuff that could error out, and his script would carry on and just produce what Grump Admin likes to call a PowerShell sea of red!
I was shocked that my friend hadn’t given any thought to error handling. If you are from a programming background, you know that exception handling is a key feature and very powerful feature, one feature that I relied on a hell of a lot to get my code working!
So the basic building blocks of exception handling in PowerShell is the TRY CATCH methodology
try {}
catch{}
This is great and captures all exceptions raised! If you had to do certain things on certain exceptions you can actually name the exception you want to CATCH in its own catch block like this!
catch [exception name goes here]
Simple isn’t it… How easy it is error handling in PowerShell! Good job all my code works all the time and never needs exception handling [sic]!!!!
So let’s do an example for you guys, Ok so I am going to run the following cmdlet. Which unless you have a crazier file system than my hair style, will throw a wonderful red exception on to your console.
get-childitem c:\grumpyadminsego
As you can see it couldn’t find grumpyadminsego! As expected and throw up a sea of PowerShell Red!
Now, let put that inside our bog standard try/catch code
Try
{
get-childitem c:\grumpadminsego -erroraction stop
}Catch
{
$_.Exception.Message
}
Now let wall through this, as you can see, I have had to put the -erroraction flag in there – this tell the cmdlet. What to do on an error such as an exception. I want it to stop! So I say… STOP!
Next, inside our catch section of code. You can see that I am accessing the default object passed ($_) to it! if you do a $_|get-member you will see all the properties and methods of this exception object. Useful?
Here you can see all that I am doing is throwing the Message property to the screen! As it is a code block you do whatever you want! Send Email , restart the server whatever you want!
Now to demo the catching a particular exception code thing! The exception thrown is the itemnotfoundexception which is buried in some long ass namespace that I can’t be bothered to type out again! I could of cut and paste it but I’m being lazy!
Ok, lazy attitude adjusted! Here it is System.Management.Automation.ItemNotFoundException it actually shorter than the text i typed in to complain about typing it in!!!! Grumpy admin is meh and is unsure if he like being nice today!
So our full exception code is like this
try
{
get-childitem c:\grumpyadminsego -erroraction stop
}
catch [System.Management.Automation.ItemNotFoundException]
{write-host “Doing Customer Error Stuff Here”
write-warning $_.Exception.Message
#write the exception so you know it works!
}
catch
{
$_.Exception.Message
}
For easy of testing, I will throw it in a function called test-error
function test-error
{
try
{
get-childitem c:\grumpyadminsego -erroraction stop
}
catch [System.Management.Automation.ItemNotFoundException]
{
write-host “Doing Customer Error Stuff Here”
write-warning $_.Exception.Message
#write the exception so you know it works!
}
catch
{
$_.Exception.Message
}
}
Call the test-error and you will see some wonderful exception handling abilities in your PowerShell functions! Enjoy
Hazzy