Automating the world one-liner at a time…
We pride ourselves about being a bit overboard when it comes to Error handling. That is why our sparse (non-existing?) documentation of THROW and TRAP handling is both ironic and sad. 10,000 apologizes for that, we'll fix it in the next release.
In the mean time, it's Don Jones amd Jeffery Hicks to the rescue. Don and Jeffery are the co-authors of Windows PowerShell: TFM. The great thing about that is that they decided to "give you a taste" of the book so you can see whether you might be interesting in buying it. It is our collective good luck that they made Chapter 11 Debugging and Error Handling as the sample chapter. Click on the Chapter in the previous sentence and it will bring you to the chapter. If that doesn't work - go to: http://www.sapienpress.com/powershell.pdf .
BTW - their book recently went to Press so it should be available any day now. I can't wait to get my copy.
Cheers!
Jeffrey Snover [MSFT]Windows PowerShell/MMC ArchitectVisit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShellVisit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
Nice but Don and Jeffery could not work out the syntax for:
Trap [ExceptionType] {
either it would seem as they don't have an example of how to express [ExceptionType]
nor does Bruce Payette
Sorry, but if they can't work it out how can I?
:J
Umm.
Try this in a script file:
trap [System.DivideByZeroException] { write-host ("trapped " + $_.Exception.GetType().Name); continue; }
(1/$null)
It works just fine
So why do you care about debugging powershell scripts? Umm unless you have the superhuman ability to
Fyi, the sample chapters at Sapien are chapter 1 and 9 (formatting) NOT chapter 11 (trap and throw).
I have to say it: LAME!
It's almost like you guys went out of your way to make sure that all the PowerShell developers and their buddies could make some extra money writing books to fill the voids in your documentation.
What's worse is that this is the first Google hit for powershell trap ... is there anywhere where there is *actual* document about it, instead of a broken link?
http://64.233.169.104/search?q=cache:vo1cixTB8RIJ:www.sapienpress.com/powershell.pdf
The book excerpt says "Trap handlers always execute, no matter what ErrorAction you specify"
It seems some commands require -ea 'stop' on the command or set globally with $ErrorActionPreference = "Stop"
How's this for an attempt?
http://huddledmasses.org/trap-exception-in-powershell/
The link to http://www.sapienpress.com/powershell.pdf is busted
The link to the pdf is not working :( Any one have another link I can try?
Cheers for all the hard work!
Zi
This is a great example of how to use Trap and Throw in PowerShell to handle errors besides using SilentlyContinue
Joel "Jaykul" Bennett, that last link to http://huddledmasses.org/trap-exception-in-powershell/ was just perfect. Thanks a ton.
Why doesn't this trap the "Generic Failure"?
====================
Function Test-Trap1() {
trap [Exception] {
write-host
write-error $("TRAPPED: " + $_.Exception.GetType().FullName);
write-error $("TRAPPED: " + $_.Exception.Message);
continue;
}
$credentials=Get-Credential 'CONTOSO106\Administrator'
if (-not $credentials) {return}
Write-Host "credentials exist."
$SqlCmd=Get-WmiObject `
-Credential $credentials `
-ComputerName 'IDM-SCCM2' `
-Namespace 'ROOT\sms\site_IDM' `
-Class 'SMS_SCI_SQLCmd'
write-host "done.";
cls
Test-Trap1
RESULTS:
credentials exist.
Generic failure
At :line:12 char:22
+ $SqlCmd=Get-WmiObject <<<< `
done.
@SMerrill8.
My guess is that the error is a NON-TERMINATING error so it is not caught by the TRAP. You can convert a non-terminating error into a terminating error (so it is caught by the TRAP) by adding "-ErrorAction STOP" to the Get-WMIOBject cmdlet.
GIve that a try and let me know what happens.
Enjoy!
Jeffrey Snover [MSFT]
Distinguished Engineer
Visit the Windows PowerShell Team blog at: blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: www.microsoft.com/.../msh.mspx