【问题标题】:Powershell generic session and import this session in Exchange remote management sessionPowershell 通用会话并在 Exchange 远程管理会话中导入此会话
【发布时间】:2014-08-25 20:57:08
【问题描述】:

情况

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

此代码非常适合简单的任务(例如“新邮箱”)!但是如何创建“通用 PS 会话”,然后在“Exchange 远程管理会话”中使用该会话?

【问题讨论】:

    标签: c# asp.net session powershell exchange-server


    【解决方案1】:

    据我了解您的问题是:

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

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

    首先,使用 ADSI(旧式)

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

    第二,从.NET 3.5开始微软引入PrincipalAccountManagement

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

    第三,可以使用低级(原生LDAP)协议

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

    【讨论】:

      【解决方案2】:

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

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

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2016-01-13
        • 2019-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-03
        • 1970-01-01
        • 2011-03-14
        • 1970-01-01
        相关资源
        最近更新 更多