Quantcast
Channel: PowerShell
Viewing all articles
Browse latest Browse all 15370

Pester and Try{} Catch{} -- Or rather, don't use $Error in modules

$
0
0

I am trying to learn pester by writing tests for an existing function I have. I want to change some of the functionality and need to make sure that I don't break anything, so adding some pester tests before modifying the function seemed like a good idea.

This section of the script has some try catches, the script is supposed to accept either a sid or a domain\username, and return the sid, it works when running the function by itself. The errors catch properly and everything:

Powershell
If($UserID){$sids=ForEach($itemin$UserID){Try{#If the value submitted isn't a valid sid, it'll error.$sid=[System.Security.Principal.SecurityIdentifier]($item)#line possible not needed:#$sid = $sid.Translate([System.Security.Principal.SecurityIdentifier])}Catch[System.Management.Automation.RuntimeException]{# If a RuntimeException...

Viewing all articles
Browse latest Browse all 15370

Latest Images

Trending Articles



Latest Images

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