Vinc 웃
Vinc 웃

Reputation: 1257

How to use ActiveDirectory module (RSAT tools) in c#

I want to use specific command that are provided by the "Active Directory Administration with Windows PowerShell". So I installed this module on my server.

Now, I want to use these commands in my code. My project is in c# - ASP.NET.

Here's the code I use to call traditional "cmdlet" command (New-Mailbox, Set-User, ...) :

string runasUsername = @"Mario";
string runasPassword = "MarioKart";
SecureString ssRunasPassword = new SecureString();
foreach (char x in runasPassword)
    ssRunasPassword.AppendChar(x);
PSCredential credentials =
new PSCredential(runasUsername, ssRunasPassword);

// Prepare the connection
var connInfo = new WSManConnectionInfo(new Uri("MarioServer"),
            "http://schemas.microsoft.com/powershell/Microsoft.Exchange",credentials);
connInfo.AuthenticationMechanism =
            AuthenticationMechanism.Basic;
connInfo.SkipCACheck = true;

connInfo.SkipCNCheck = true;

// Create the runspace where the command will be executed
var runspace = RunspaceFactory.CreateRunspace(connInfo);

//Params
....

// create the PowerShell command
var command = new Command("New-Mailbox");
command.Parameters.Add("Name", name);
....

// Add the command to the runspace's pipeline
runspace.Open();
var pipeline = runspace.CreatePipeline();
pipeline.Commands.Add(command);

// Execute the command
var results = pipeline.Invoke();

if (results.Count > 0)
     System.Diagnostics.Debug.WriteLine("SUCCESS");
else
     System.Diagnostics.Debug.WriteLine("FAIL");

runspace.Dispose();

This code work perfectly ! Great ! But let say I want to use "Set-ADUser", this command is from ActiveDirectory module (RSAT tools).

Given that all is set on the server (the module is installed), I tried to simply change "New-Mailbox" for "Set-ADUser" :

var command = new Command("Set-ADUser");

When I run the code, I have this error :

The term 'Set-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program.

So, that's my question :

How can we run command from the ActiveDirectory module (RSAT tools) in c# ? (Im using VS 2010).

Upvotes: 1

Views: 2119

Answers (1)

user189198
user189198

Reputation:

As @JPBlanc pointed out in the comments section, you will need to ensure that the ActiveDirectory PowerShell module is loaded. PowerShell version 3.0 and later have module auto-loading enabled by default (it can be disabled), but if you're still targeting PowerShell 2.0, then you must first call:

Import-Module -Name ActiveDirectory;

.. before you can use the commands inside the module.

For the purposes of validation, you can use the Get-Module command, to ensure that the ActiveDirectory module has been imported.

Get-Module -Name ActiveDirectory;

If the above command returns $null, then the module is not imported. To verify that PowerShell can "see" the ActiveDirectory module, without actually importing it, run this command:

Get-Module -Name ActiveDirectory -ListAvailable;

Upvotes: 1

Related Questions