繁体   English   中英

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

[英]Powershell generic session and import this session in Exchange remote management session

情况

我正在尝试创建一个应用程序(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");

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

据我所知,你的问题是:

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

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

,使用ADSI(旧时尚)

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

,开始.NET 3.5 Microsoft引入了PrincipalAccountManagement

如何使用C#在Active Directory上几乎所有(使用AccountManagement)。

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

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

你不完全正确。 由于您要连接到REMOTE PowerShell会话,因此无需执行导入模块。

创建会话时,IIS和Powershell将根据与凭据关联的访问权限自动为您加载所需的模块。 这与加载Exchange Powershell并查看页面加载模块顶部的绿色栏类似。

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM