Managing required Snapins

I’m often hesitant to rely on snap-ins in my production scripts.  My fear is that a Jr Sysad will grab the script and attempt to run it.   Without the required Snap-ins the script will fail, and that failure will be “my fault”.  I solved this little problem by adding the following to any scripts that requires snap-ins.

write-Host “Loading required PSSnapins…”
# Load the PSCX if they are not already loaded…
if (!(Get-PSSnapin | ?{$ -eq “pscx”})){Add-PSSnapin pscx -ErrorAction Stop}

Now when the script fails I can at least point at the error, and say “what do you mean you don’t know why it’s not working, it’s right there!”

Thank you aleksandar !
Talk about doing it the hard way, all I had to do was add the “#requires” header to the first line of my scripts.

#requires -pssnapin pscx

Gotta love PowerShell every time you think you solved a problem, someone points out a feature of the parser that handles it natively!


2 thoughts on “Managing required Snapins”

Leave a Reply