Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell generic session and import this session in Exchange remote management session

The situation :

I am trying to make an application (c#-asp.net) to manipulate user's on an exchange server. The application will be on a different server than the exchange's one. So, to manipulate the data, I am using an "Exchange remote management session" created with c#. Exchange remote management session give access to simple powershell command like "New-Mailbox" and "Set-User" - This is good for simple task, but in my case, I have to do more complexe operations that will need some specific command that is not included in the default command. To access this command, I have to use some specific module like "ActiveDirectory". It is simple ? Only use "Import-Module" ! Not really, like I said, the "Exchange remote management session" is very limited with the command, and "Import-Module" is not allowed...

So what we can do ?

I read a lot about my problem, and the most "simple" (That I understand the theory) solution is something like :

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.

Given that I am pretty new to manipulate the Powershell with c#, I have no idea how to use this awesome solution in my code. So I am asking your help.

Here's my current code :

// Prepare the credentials.
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("New-Mailbox");
....

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

This code work great for simple task (like "New-Mailbox") ! But how can I create a "generic PS session" and then use this session in the "Exchange remote management session" ?

like image 510
Vinc 웃 Avatar asked Nov 01 '22 21:11

Vinc 웃


1 Answers

As far a I understand your problem is :

You have to do more complex operations that will need some specific command that is not included in the default command. To access this command, You have to use some specific module like "Active Directory".

Here are three ways to query Active Directory using C# without PowerShell module).

First, using ADSI (old fashion)

How to do Almost everything (with ADSI) on Active Directory with C#.

Second, beginning .NET 3.5 Microsoft introduce Principal and AccountManagement.

How to do Almost everything (with AccountManagement) on Active Directory with C#.

Third, you can use low level (native LDAP) protocol

System.DirectoryServices.Protocols (S.DS.P).

like image 130
JPBlanc Avatar answered Nov 12 '22 18:11

JPBlanc