Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2314

PowerShell: Handle #requires-statements that are not met

$
0
0

Hi there,

currently I'm writing on a script in which I'd like to handle some Basic requirements using the new #requires-statements. For example:

#requires -RunAsAdministrator

My question is that I'd like to catch the exception if an error is not met and write an entry to the Windows EventLog so that I can see it in our Monitoring.

I already tried enclosing the #requires-statement in a try...catch block and reading the error from the $Error Array. Both of these ways don't work. The only way I could imagine at this time is to have awrapper script that dot-sources the original script and then going through the $Error Array.

Is there any way that is more elegant?

Thanks in advance.

Yours sincerely

powershellking


Vielen Dank powershellking


Viewing all articles
Browse latest Browse all 2314


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