Ezine 205 – PowerShell’s Snapins

Ezine 205 – PowerShell’s Snapins

The killer reason for adding PowerShell snapins is to view or change Active Directory objects such as users.  If you want to check a user’s properties with the native PowerShell commands you will find the commands cumbersome, whereas the QADUser cmdlets are slick.  It is often claimed that there are other useful snapins, however I have not found any!

PowerShell Topics for Profile.ps1 and Add-PSSnapin

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

 ♣

This Week’s Secret

If you take the opportunity to explore PowerShell’s add-PSSnapin then it will open a new world of cmdlets such as QAD’s Active Directory family.  Furthermore, get-PSSnapin will encourage you to research profile.ps1, which controls what happens when PowerShell launches.

Example 1 – Add-PSSnapin

Pre-requisites
The only slight problem with this challenge is that it needs two pre-requisites over-and-above installing PowerShell.  Firstly, download the snap-in from the third party such as Quest.  Because without a snap-in the command, add-PSSnapin makes no sense.  Your plan B would be to substitute one of the growing number of third-party snap-ins for Quest.

Secondly, to get any action from the additional Quest active directory snap-in you need to logon as administrator of a domain.  I downloaded and installed the snap-in and then I ran the following command on a Domain Controller.  I say again, if you don’t have a domain, research the internet for alternative snap-ins.

# Simple Instruction to Introduce More Cmdlets
set-Location c:\
add-PSSnapin  quest.activeroles.admanagement

Note 1:  Please navigate to wherever you downloaded the snap-in.  I used the c:\ root for simplicity, but your path is likely to be different, hence adjust set-Location in the above script.

Note 2:  If these commands don’t reveal the quest snap-ins, you probably missed one of my pre-requisites.  As I mentioned earlier, you must download and install the quest.activeroles.admanagement snap-in from Quest’s site, and you need access to an active directory domain.

Example 2 – Check Your Registered Snap-ins

If you substitute the verb ‘get’ for ‘add’, then PSSnapin will display all registered snap-ins including the Microsoft default sets.

# Optional instruction to display all Snap-ins
Get-PSSnapin  | ft name

Expected results:

PowerShell  1.0 November 2008

Name
————————-
Microsoft.PowerShell.Core
Microsoft.PowerShell.Host
Microsoft.PowerShell.Management
Microsoft.PowerShell.Security
Microsoft.PowerShell.Utility
quest.activeroles.admanagement

PowerShell 2.0  May 2010

Name
————————
Microsoft.PowerShell.Core
Microsoft.PowerShell.Diagnostics
Microsoft.PowerShell.Host
Microsoft.PowerShell.Management
Microsoft.PowerShell.Security
Microsoft.PowerShell.Utility
Microsoft.WSMan.Management
quest.activeroles.admanagement

Note 1:  Did you get just  the Microsoft snap-ins?  Or did you get a snap-in from quest?

Note 2: In PowerShell 2.0 I also added remoting, hence the WSMan snapin.

PSSnapins and Profile.ps1

As we will see, the killer reason for mastering profile.ps1 is so that you can load PSSnapins.  As a result of this procedure PowerShell will load extra cmdlets effortlessly each time it starts.

Once you achieve mastery in a particular area, any difficulties that you endured during the learning process seem to fade into the background.  So it is with understanding Profile.ps1.  Configuring this file is ridiculously easy once you have mastered it, but impenetrably difficult when you try and figure it out for the first time.

Pure Profile.ps1

The secret of setting-up profile.ps1 is attention to spelling.  In particular, the filename ‘profile’ must be singular, and the directory name must be precisely: ‘WindowsPowerShell’.

Let me give you a few pointers with the text file profile.ps1.  The file must be called precisely profile.ps1.  The name profileS.psL would be doubly wrong  because of the plural filename and ‘L’ instead of a 1 in the extension.

You must save profile.ps1 into a specific directory.  Begin your drill-down by navigating to the C:\Documents and Settings folder.  Next you need to know the Windows profile name for the user who is logged on, in my case \guyt.  The rest of the path is My Documents\WindowsPowerShell.  Pay close attention to the folder name: WindowsPowerShell.  Here is my full path.
C:\Documents and Settings\guyt\My Documents\WindowsPowerShell

It is in the above WindowsPowerShell folder that I save profile.ps1.

There is a whole micro-industry in creating clever instructions for profile.ps1, (just as there was tweaking the old DOS autoexec.bat).  For our mission we will focus on just two instructions, setting the HOME directory and loading snap-ins.

# Simple instruction for Profile.ps1 (Feel free to amend c:\)
set-Location c:\

I always like to build on success, thus I include the simple command, ‘set-Location c:\’ merely to show that PowerShell is in fact reading profile.ps1.  If this works and PowerShell opens at the c:\ prompt, then you could try a more realistic ‘home’ directory for your PowerShell, for example, set-Location c:\PowerShell (Providing you have a folder called precisely, PowerShell).

Summary of PowerShell’s Add-PSSnapin

If you need to employ PowerShell to manipulate active directory, then seek additional cmdlets.  The best the best sources are Quest and PowerGUI.  Additionally, if you need to control PowerShell’s start-up environment, then take the time to create and configure profile.ps1.

Once you have installed the snap-ins, you need a command to load them each time PowerShell launches, that command is : add-PSSnapin.  Why not get the best of both worlds?  Master profile.ps1 and make sure that the quest.activeroles.admanagement snap-in is always available.

See more about PowerShell cmdlets

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials

PowerShell Tutorials  • Methods  • Cmdlets  • PS Snapin  • Profile.ps1  • Exchange 2007

Command & Expression Mode  • PowerShell pipeline (|)  • PowerShell ‘where‘  • PowerShell ‘Sort’

Windows PowerShell Modules  • Import-Module  • PowerShell Module Directory 

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links.