Asked By Kirk Munro [MVP]
20-Jul-08 12:39 PM

Hi Keith,
I typed up a great reply to this earlier but I think I hit respond to sender
and not respond to group, and it went off into the ether.
There are a few things you need to be aware of when using cmdlets that are
included in 3rd party snapins. When organizations publish 3rd party snapins
they are often accompanied by a custom console that automatically loads that
snapin so that you can get access to those cmdlets easily. But when you
want to use the console that came with PowerShell, you need to tell it to
load that snapin yourself. For the AD cmdlets you do so like this:
Add-PSSnapin Quest.ActiveRoles.ADManagement
That loads the Quest cmdlets into the PowerShell environment so that you can
use them.
You can also update your user profile so that it loads the cmdlets every
time you open PowerShell like this:
1. Run 'notepad $profile' in PowerShell (without the quotes)
2. If you are prompted to create a new file, create it.
3. Paste the Add-PSSnapin command listed above into that profile file and
save it.
Now every time you open your PowerShell console, the Quest AD snapin will
load automatically. That works great for your personal user account, but
if you are writing a script that you want to run using other accounts via a
scheduler (which you are in this case) or if you are running this script
between machines, you might want to make the script load the snapins
automatically if it is not loaded. To do that, add this to the top of your
script:
if (-not (Get-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction
SilentlyContinue)) {
Add-PSSnapin Quest.ActiveRoles.ADManagement
}
And of course, with PowerShell there are other ways too. You can launch
PowerShell.exe and specify which snapins you want automatically loaded by
using a psc1 file (a PowerShell Console file). This file is an xml document
that identifies which snapins should be loaded when the console starts.
You should probably read this blog article about custom PowerShell consoles
to understand how they work so that you can move into the regular PowerShell
console and continue using non-default snapins:
http://poshoholic.com/2008/05/20/essential-powershell-use-non-portable-console-customizations-sparingly/
Now, as for executing your script in the scheduler, you'll want to launch it
by setting up a task to run powershell.exe with a few parameters. You can
see how to launch a script file this way in this blog article:
http://poshoholic.com/2007/09/27/invoking-a-powershell-script-from-cmdexe-or-start-run/
And if you want to use a psc1 file, you can just look at the shortcut for
the custom console that comes with the Quest AD cmdlets and see how it works
(and you can even use the same psc1 file it comes with if you like).
--
Kirk Munro [MVP]
Poshoholic
http://poshoholic.com
groupe de discussion : 95E4F9E9-7490-4D3B-A0AF-14D2D1BD18D3@microsoft.com...