Reputation: 949
I have a script which are going to run on several local computers, but I haven't done any errorhandling in it. I have not done any errorhandling in powershell at all, so i'm a total noob here. I read something about it, but honestly I am just looking for an quick and easy answer..
Q: Is there something like a try/catch, as little code as possible to not make the script heavy weight?
Upvotes: 1
Views: 767
Reputation: 282
Powershell 2.0 has try, catch and finally. See http://technet.microsoft.com/en-us/library/dd315350.aspx You can also do a more command-line oriented variation of error handling with error trapping like here: http://www.informit.com/articles/article.aspx?p=729515&seqNum=5
Upvotes: 0
Reputation: 3429
Yes, try/catch/finally is available in Powershell v2:
Try{
<main code here>
}
Catch{
write-host $_ #Using the reserved $_ variable which should contain the error string
}
Finally{
<clean up code here - will execute regardless>
}
If you're constrained to use Powershell v1 then you will have to use the trap construct.
Upvotes: 2