SCRT-HQ / PSGSuite

Powershell module for Google / G Suite API calls wrapped in handy functions. Authentication is established using a service account via P12 key to negate the consent popup and allow for greater handsoff automation capabilities
https://psgsuite.io/
Apache License 2.0
235 stars 67 forks source link

Errors do not trigger try{}catch{} statements #217

Closed Chris4678 closed 5 years ago

Chris4678 commented 5 years ago

I am trying to build some error handling into my code. Normally try and catch statements are very handy for this. However, if you do something like my simple code below it will never actually trigger the catch statement. Is there a work around for this? How should I catch errors?

Try { Update-GSUser -User email@domain.com -OrgUnitPath $InvalidOUPath } catch { "unable to update $user's OU" | Out-File $log -append }

scrthq commented 5 years ago

Hey @Chris4678 - If you add -ErrorAction Stop to your Update-GSUser call, it should trigger a terminating error and catch it appropriately. Let me know if your experience is different!

Chris4678 commented 5 years ago

Hello Scrthq. Thanks for the quick response. I have added that to all of my commands now. Thanks a lot for building this module, its awesome!

scrthq commented 5 years ago

@Chris4678 I'm glad you're enjoying it!! <3