1

我有这个代码,我向我的交换服务器(server01)远程发送了一个powershell命令“date”并且它工作正常,我在一个消息框中收到了一个结果。

但是,如果我发送命令“Get-Mailbox”,调试器会因以下错误而停止:“Get-Mailbox”一词未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。

如果我转到 server01 并运行 powershell 并执行“Get-Mailbox danielr”,则与我得到的错误相同。但是 Exchange 命令行管理程序可以很好地执行 Get-Mailbox 命令。

因此,我想我已连接到 Window Powershell cmd..但是,要执行“Get-Mailbox danielr”和其他 Exchange 管理命令,我必须连接到 Exchange 命令行管理程序。

我需要进行哪些更改才能使其正常工作?(连接到 Exchange 管理 shell,而不是 powershell。非常感谢!!

public void CreateMailBoxExchange()
    {
        string loginName = "administrator"; 
        string loginPassword = "123456";
        SecureString ssLoginPassword = new SecureString();
        foreach (char x in loginPassword)
            ssLoginPassword.AppendChar(x);


        PSCredential remoteMachineCredentials = new PSCredential(loginName, ssLoginPassword);

        // Set the connection Info
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://server01:5985/wsman"), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", remoteMachineCredentials);

        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Negotiate;
        //connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

        Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);

        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();
        command.AddCommand("date");
        //command.AddCommand("Get-Mailbox");
        //command.AddParameter("Identity", "danielr");

        powershell.Commands = command;
        try
        {
            // open the remote runspace
            runspace.Open();
            // associate the runspace with powershell
            powershell.Runspace = runspace;
            // invoke the powershell to obtain the results
            powershell.Invoke();
            var results = powershell.Invoke();
            runspace.Close();
            foreach (PSObject obj in results)
            {
                StringBuilder stringBuilder = new StringBuilder();
                //stringBuilder.AppendLine(obj.ToString());
                MessageBox.Show(obj.ToString());
            }

        }
        finally
        {
            // dispose the runspace and enable garbage collection
            runspace.Dispose();
            runspace = null;
            // Finally dispose the powershell and set all variables to null to free
            // up any resources.
            powershell.Dispose();
            powershell = null;
        }
4

3 回答 3

1

根据您运行的 Exchange 版本,您可能需要执行以下代码:

对于 2007 年

Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.Admin

对于 2010 年

Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.E2010

对于 Exch 2013(试试这个)

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
于 2014-09-02T16:45:14.267 回答
0

如果您远离您的代码建议的 Exchange Server,那么您必须使用类似的东西:

string loginName = "administrator"; 
string loginPassword = "123456";
SecureString ssLoginPassword = new SecureString();
foreach (char x in loginPassword)
    ssLoginPassword.AppendChar(x);

PSCredential remoteMachineCredentials = new PSCredential(loginName, ssLoginPassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://server01:5985/Powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", remoteMachineCredentials);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Negotiate;
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo); 
于 2014-09-02T16:52:26.793 回答
0

在尝试 Get-Mailbox 命令之前,您应该能够只包含 Exchange 管理管理单元。以下是我在 PowerShell 中执行此操作的方法:

Add-PSSnapin "Microsoft.Exchange.Management.PowerShell.Admin"
于 2014-09-02T16:43:27.323 回答