Search code examples
c#powershellexchange-serverwindows-server-2012

Import-Module is not recognized - Powershell with c#


I am facing a very strange situation.

I want to use the command "Import-Module" to import the "ActiveDirectory" module. The first thing I did is to install the module on my server (windows server 2012 R2). Once I completed the first step, I tried to call the command "Import-Module" with the param "ActiveDirectory".

Here's the code I am using :

// Prepare the credentials that will be used when connecting
// to the server. More info on the user to use on the notes
// below this code snippet.
string runasUsername = @"MarioKart 8";
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("MarioKart8Server"),
   "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);

// create the PowerShell command
var command = new Command("Import-Module");
command.Parameters.Add("Name","ActiveDirectory"); //I tried new string[] {"ActiveDirectory"} too.

// 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");

Ok, so when I run this, I have this exception :

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

Note that I can run basic command like "New-Mailbox" without any problems, so the problem is really the "Import-Module".

I really don't know what to do, I did a lot of research and it look like some people have the same problem, but I did not find any documented solution.

Note that when I run the "Import-Module" directly on the server's powershell, all is working !

I read that this is probably a problem about the powershell version, the version I am using is 4.0 (on the server).


Solution

  • I suspect you're working with an Exchange remote management session. If you are, that is a "No Language" constrained session. It will have the Exchange cmdlets, and a few of the generic Powershell commands (like Get-Command) available, but it does not have the Import-Module cmdlet.

    IMHO, the easiest way to have both sets of cmdlets would be to start with a generic PS session, import the AD module, then connect to an Exchange management session and do an Import-PSSession and use implicit remoting for the Exchange management stuff. If you do it this way, you aren't limited to just running it on an Exchange server, and don't have to have the Exchange management tools installed.

    Edit: The Powershell commands to do this would be:

    Import-Module ActiveDirectory
    
    $EXSession = new-pssession -configurationname Microsoft.Exchange -ConnectionURI http://<Exchange server name>/powershell/ -authentication kerberos 
    
    Import-PSSession $EXSession
    

    Not that you'll have to provide the name of an Exchange server in your environment to connect to.