Tags


Logo_PowerShell

Here’s one that drove me nuts and I finally decided “I’ve had enough! I’m going to sit down and tell others how to avoid this problem!”

When you work with Powershell and various applications you have one BIG Bane to deal with.

“Powershell Snapins”

They are the STRENGTH and the WEAKNESS to your Powershell scripts.  

Strength because of the abilities they give you and Weakness because unless you have a Shell like I do where all the Snapins are just LOADED up and ready to go?   You’ll get lots of Red errors as the Script tries to execute CmdLets it doesn’t know about yet because the darn Snapin didn’t get loaded up for that session.

And honestly, loading up a Pile of UnNeeded snapins just Bogs things down.

And the solution is very simple.  Add a few lines to the top of your script to CHECK for the Snapins that you need first, try to add them in.   Of course we should document the heck out of our scripts anyhow but shouldn’t the SCRIPT be doing the work for us?

So at the top of my scripts that use the Quest Active Roles Management CmdLets I’m going to be adding these few lines.  Their job is simple.  Check for the Snapin, if it’s not there, load it, if is go one, if it can’t BE loaded (say you errrrr don’t actually HAVE the management Snapin?) throw an error to notify the person running the script.

 

So all you have to do is run

 

ADD-PSSNAPIN NameOfSnapin –erroraction SilentlyContinue

 

And try to install the Snapin first. the “-erroraction SilentlyContinue” will hide those nasty Red letters if things went bad and it’s not there to load.

 

GET-PSSNAPIN NameOfSnapin –erroraction SilentlyContinue

 

and we can check the status of the snapin.  If it loaded ok (We had the module) we’ll get results, otherwise the results are $NULL and we can inform the user.

 

So now my Scripts (since they rely on the Quest Active Roles Management Powershell Snapin) will look like THIS to ensure that no matter what machine I’m on, It will check, load and warn before I run the script and see a kabillion red errors because I forgot to type something

 

It might seem cumbersome, but if you get into this habit your scripts will be a lot more portable and friendlier for other admins to use.

And REMEMBER!  You DON’T have to have Powershell and your Management tools on the SERVER just to use them!  Almost EVERY SINGLE CMDLET will manage from the Workstation, albeit just a bit slower.

 

Rock on with Powershell

 

Sean
The Energized Tech

————————- BeginningOfGenericScript.PS1————————————————-

#
# Add in Snapins Before launching Script.  I’ve written this, so you could run a pile of these checks
# and until it’s all ‘GOOD’ the script will quickly fail out.
#
#
# Variable to remember the $STATUS of your modules loaded

$STATUSLOADED=$FALSE

# The name of whatever snapin being dealt with

$SNAPIN=’Quest.Activeroles.ADManagement’

# Try to add the Snapin and be VEWY QWIET if it DOESN’T load
#
ADD-PSSNAPIN $SNAPIN –erroraction SilentlyContinue
#
# Check to see if it successfully loaded
#
IF ((GET-PSSNAPIN $SNAPIN) –eq $NULL)
     {
          # If not loaded – Notify user with required details
          #
          WRITE-HOST ‘This Script requires Quest ActiveRoles Management Shell’
          WRITE-HOST ‘Which can be downloaded free from http://www.quest.com/powershell/activeroles-server.aspx’
     }
ELSE
    {
          # If it DID, Flag Status as GOOD
          #
          $STATUSLOADED=$TRUE
     }

# And we treat the WHOLE script as an “IF” to make it work only “IF”
# the require modules have been loaded!
#
IF ($STATUSLOADED)

{

# Begining of your normal script here

}

————————- BeginningOfGenericScript.PS1————————————————-

Advertisements