C#已连接到Powershell,但我需要连接到Exchange命令行管理程序

时间:2014-09-02 16:23:53

标签: c# powershell exchange-management-shell

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

但是,如果我发送命令" Get-Mailbox" debbugers停止了这个错误:术语' Get-Mailbox'不被识别为cmdlet,函数,脚本文件或可操作程序的名称。

如果我去server01并运行powershell并执行" Get-Mailbox danielr"是我得到的同样的错误。但是Exchange命令行管理程序执行Get-Mailbox命令很好。

所以,我想我已经连接到Window Powershell cmd ..但是,执行" Get-Mailbox danielr"和其他Exchange管理命令我必须连接到Exchange命令行管理程序。

我需要更改什么才能使其有效? (连接到Exchange管理外壳,而不是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;
        }

3 个答案:

答案 0 :(得分:1)

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

对于Exch 2007

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

2010年Exch

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

对于Exch 2013(试试这个)

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn

答案 1 :(得分:0)

在尝试Get-Mailbox命令之前,您应该只能包含Exchange管理管理单元。以下是我在PowerShell中的表现:

Add-PSSnapin "Microsoft.Exchange.Management.PowerShell.Admin"

答案 2 :(得分: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);