Powershell通用会话并在Exchange远程管理会话中导入此会话

Vin*_*c 웃 5 c# asp.net powershell session exchange-server

情况:

我正在尝试创建一个应用程序(c#-asp.net)来操纵交换服务器上的用户.该应用程序将位于与交换机不同的服务器上.因此,为了操纵数据,我正在使用用c#创建的"Exchange远程管理会话".Exchange远程管理会话可以访问简单的powershell命令,如"New-Mailbox"和"Set-User" - 这对于简单的任务很有用,但在我的情况下,我必须做更多复杂的操作,需要一些特定的命令,不包含在默认命令中.要访问此命令,我必须使用某些特定模块,如"ActiveDirectory".很简单 ?只使用"导入模块"!不像我说的那样,"Exchange远程管理会话"对命令非常有限,并且不允许使用"Import-Module"...

那我们能做什么呢?

我读了很多关于我的问题,而最"简单"(我理解理论)的解决方案是这样的:

从通用PS会话开始,导入AD模块,然后连接到Exchange管理会话并执行Import-PSSession并对Exchange管理内容使用隐式远程处理.

鉴于我很擅长用c#操作Powershell,我不知道如何在我的代码中使用这个很棒的解决方案.所以我在问你的帮助.

这是我目前的代码:

// 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");
Run Code Online (Sandbox Code Playgroud)

此代码适用于简单的任务(如"New-Mailbox")!但是,如何创建"通用PS会话",然后在"Exchange远程管理会话"中使用此会话?

JPB*_*anc 2

据我了解你的问题是:

您必须执行更复杂的操作,这些操作将需要一些默认命令中未包含的特定命令。要访问此命令,您必须使用某些特定模块,例如“Active Directory”。

以下是在不使用 PowerShell 模块的情况下使用 C# 查询 Active Directory 的三种方法。

首先,使用ADSI(老式)

如何使用 C# 在 Active Directory 上完成几乎所有操作(使用 ADSI)

其次,微软开始引入.NET 3.5Principal和.NET 3.5 AccountManagement

如何使用 C# 在 Active Directory 上完成几乎所有操作(通过 AccountManagement)。

第三,您可以使用低级(本机 LDAP)协议

系统.目录服务.协议 (S.DS.P)。