d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Try - Catch
Add Reply New Topic New Poll
Member
Posts: 22,269
Joined: May 26 2007
Gold: 100.24
May 16 2013 12:56am
Hello,

I was just wondering how try-catch works in powershell. Let's take the code below:

Code
$ErrorActionPreference = "Stop";
try {
   Get-ActiveSyncDevice -Mailbox $name  | % {
     Remove-ActiveSyncDevice $_ -Confirm:$false   #remove activesync devices from user object
     }
}
catch {
#no activesync devices
}
finally{
$ErrorActionPreference = "Continue";
}



What I want to do, is that if there is no activesync device, no error message gets shown in powershell. Is this correct way or do I have to change something? ^_^
Member
Posts: 22,269
Joined: May 26 2007
Gold: 100.24
May 20 2013 05:57am
This seems more complex than I thought, no answer yet. Gotta find time to read more documentation some day.
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
May 20 2013 12:17pm
Here is some great example code, oh wait, I'm not allowed to post links cuz I'm a very bad person!

google "Hey, Scripting Guy! How Can I Use Try/Catch/Finally in Windows PowerShell?"

This post was edited by Azrad on May 20 2013 12:19pm
Member
Posts: 22,269
Joined: May 26 2007
Gold: 100.24
May 21 2013 04:16am
Quote (Azrad @ 20 May 2013 20:17)
Here is some great example code, oh wait, I'm not allowed to post links cuz I'm a very bad person!

google "Hey, Scripting Guy! How Can I Use Try/Catch/Finally in Windows PowerShell?"


this doesn't really help. If answer to everything is google, they might as well shut down this subforum. I have build try/catch, but I still get error messages printed out.
Member
Posts: 2,579
Joined: Jun 1 2012
Gold: 1,524.00
May 21 2013 11:38am
I've never used this language, but it appears you need to declare what kind of exception you want to catch, ex.:

Try
{
// Do Stuff
}
Catch [System.Management.Automation.PSArgumentException]
{

]
Catch [system.exception]
{

}
Member
Posts: 40,064
Joined: Nov 22 2009
Gold: 0.00
May 21 2013 11:59am
Quote
this doesn't really help. If answer to everything is google, they might as well shut down this subforum. I have build try/catch, but I still get error messages printed out.


actually it does help. if you took the 20 seconds to actually google his search term he gave you it would lead you to a tech blog which tells you that you need to include catch cases within your catch line, such as `system.exception`. the layout is much like schwag posted and is pretty much common sense if you programed in any other language which uses try catch final based error handling.

if powershell is anything like the other languages you should be able to do this

Code

Catch [system.exception e] {
  //print something with e.message or w/e
}


next time take the time to read and follow the post when someone decides to help you rather than trying to be foon fed. also posting any link outside of the jsp domain is now a suspendable offence which is loosly moderated to everyone but a select few people who the mods seem to hate. so deal with it.

-Ducks with Abs 40 days left
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
May 21 2013 08:52pm
Quote (Funion @ May 21 2013 10:59am)
also posting any link outside of the jsp domain is now a suspendable offence which is loosly moderated to everyone but a select few people who the mods seem to hate. so deal with it.
yeah everytime i post one i get smacked down. Even if I'm just quoting someone else who posted it (who didn't get smacked down) :wacko:

hey its d2jsp, logic does not apply here

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll