.NET Framework - Executing PS1 file with AD MS cmdlets

Asked By Keith
19-Jul-08 10:48 AM
Not having worked with the Active Directory Management Shell or Power Shell
much, if I want to have a scheduled task run nightly on one of my servers,
how would I best execute my ps1 file with AD MS cmdlets?  The server has
Power Shell 1.0, PowerGUI, and AD MS installed.  It wont launch properly in
Power Shell and when I try to associate the file with the AD MS shortcut it
just won't run.
Quest.ActiveRoles.ADManagement
(1)
Active Directory
(1)
PowerGUI
(1)
PSSnapin
(1)
SilentlyContinue
(1)
ScriptFanatic
(1)
ActiveRoles
(1)
KeithK
(1)
  Shay Levy [MVP] replied...
20-Jul-08 09:49 PM
Hi KeithK,

Not sure do you mean by 'AD MS installed', I guess you are referring to Quest
AD cmdlets. Anyhow, you should load the AD snap-in first and
then call any AD related cmdlets:



Add-PSSnapin Quest.ActiveRoles.ADManagement
...
AD commands
...
...



---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic



K> Not having worked with the Active Directory Management Shell or Power
K> Shell much, if I want to have a scheduled task run nightly on one of
K> my servers, how would I best execute my ps1 file with AD MS cmdlets?
K> The server has Power Shell 1.0, PowerGUI, and AD MS installed.  It
K> wont launch properly in Power Shell and when I try to associate the
K> file with the AD MS shortcut it just won't run.
K>
  Brandon [MVP] replied...
20-Jul-08 11:02 AM
I believe he talking about the ActiveRoles product from Quest (I think it
uses the CMDlets, but still is a little different.)

I think is called Active Directory Management Shell
http://www.quest.com/activeroles-server/powershelldemo/chap00.htm
  Kirk Munro [MVP] replied...
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...
Create New Account
help
cmlets machine mem = 1 gb As i do a search and detect OS via the Active Directory, i looked at the task manager and the PF Usage keeps increasing until powershell fails each sucessful run when i scan "each" machine, rather than hold it forever ? Powershell Discussions Active Directory (1) QADComputer (1) UserAccountControl (1) SilentlyContinue (1) LocalMachine (1) PsSnapin (1) Summarypath (1) RunrunMy (1 vmware guest or physical host etc cls $quest = get-PsSnapin -registered | where {$_.name -eq 'Quest.ActiveRoles.ADManagement'} | foreach {$_.name} if ($quest -eq $null) {write-host "Please install Quest add-ons to Powershell";exit} else {Add-PsSnapin Quest.ActiveRoles.ADManagement} $erroractionpreference = "SilentlyContinue" $computernames = Get-QADComputer -osName *server* -sizelimit -includedproperties userAccountControl | where {$_.useraccountcontrol
at least 1 uppercase letter, at least 2 numbers) and set it for user in active directory. And also display this password to me and log it to txt file. Powershell Discussions a look at the password setting part and tell me what's wrong: Add-PSSnapin Quest.Activeroles.ADManagement $maximumPasswordAge = (Get-QADObject (Get-QADRootDSE).defaultNamingContextDN).maximumPasswordAge.value.days if(!$maximumPasswordAge){ throw "Domain 'MaximumPasswordAge'password be better to use Levy's random password generator. Here is fixed variant: Add-PSSnapin Quest.Activeroles.ADManagement $RandomPassword = [System.Web.Security.Membership]::GeneratePassword(8, 2) function Send-Mail { param($smtpServer, $from, $to
Quest powershell v 1.2 .NET Framework Get-PSSnapin -registered Name : Quest.ActiveRoles.ADManagement PSVersion : 1.0 Description : Registers the CmdLets and Providers in this assembly I hope the yesterday and override previous version. btw what are the changes in v1.2 ? Powershell Discussions Quest.ActiveRoles.ADManagement (1) Windows Server (1) PSToolbar (1) PSSnapin (1) ScriptFanatic (1) CmdLets (1) Frameworks (1) Siddaway Director - http: / / www.powershellcommunity.org *Blog - http: / / marcoshaw.blogspot.com Try this: PS > Get-PSSnapin Quest.ActiveRoles.ADManagement | select version Version - -- -- -- 1.1.2.761 - -- Shay Levy Windows PowerShell MVP http: / / blogs
How to access Active Directory from web application? .NET Framework Hi How should I go about accessing a Active Directory from a web application? I need to display a list of users from a Active Directory on a public website. Since the web application and the Active Directory are on separate domains I don't think the web application can access the Active