Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

How to deal with Exceptions and -ErrorAction SilentlyContinue

$
0
0

Hi all.

I have stumbled upon a problem with Exception handling and I am a bit lost.

I am in the process of fixing several Powershell modules to make Cmdlets throw exceptions instead of erroring to the console. Previously, all these Cmdlets reported errors by means of return codes (horror!).

So far so good. The real problem is that most existing modules are full of this kind of code:

# Check if user exists
$user = Get-AdUser $user -ErrorAction SilentlyContinue

if($user) {
	#do something with $user
}

Now, if I wrap this kind of code into a try...catch block I still get an exception!

[PS] C:\> try {>> get-aduser dfjgdfjgj -erroraction silentlycontinue>> } catch {>> write-host "exception caught">> }>>
exception caught
[PS] C:\>

Since this kind of check is all over the code, wrapping all of these in try...catch blocks would be a MASSIVE job. I am wondering if there is a way to make -ErrorAction SilentlyContinue not raise the exception at all.

Has anyone had this kind of issue before? Has anyone got any idea?

Thanks!


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>