简体   繁体   English

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

[英]C# Connected to Powershell, but I need connect to Exchange Management Shell

I have this code, I send remotely a powershell command "date" to my exchange server (server01) and it works, I'm receiving a result in a messagebox. 我有这段代码,我向我的交换服务器(server01)远程发送了一个powershell命令“日期”,并且它有效,我在消息框中收到结果。

but, if I send the command "Get-Mailbox" the debbugers stops with this error: The term 'Get-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. 但是,如果我发送命令“ Get-Mailbox”,则调试器将因以下错误而停止:术语“ Get-Mailbox”无法识别为cmdlet,函数,脚本文件或可运行程序的名称。

If I go to the server01 and runs powershell and execute "Get-Mailbox danielr" is the same error that I get. 如果我转到server01并运行powershell并执行“ Get-Mailbox danielr”,则将得到与我相同的错误。 But Exchange Management Shell execute the Get-Mailbox command fine. 但是Exchange命令行管理程序可以很好地执行Get-Mailbox命令。

So, I guess I'm connected to the Window Powershell cmd..but, to execute the "Get-Mailbox danielr" and others Exchange Management commands I have to connect to the Exchange Management Shell. 因此,我想我已经连接到Window Powershell cmd ..但是,要执行“ Get-Mailbox danielr”和其他Exchange管理命令,我必须连接到Exchange命令行管理程序。

What do I need to change to make it works? 我需要进行哪些更改才能使其正常工作? (to connect to the exchange management shell, not to powershell. THANKS A LOT!! (连接到交换管理外壳,而不是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;
        }

Depending on which version of Exchange you are running, you may need to execute the following code: 根据运行的Exchange版本,您可能需要执行以下代码:

For Exch 2007 对于Exch 2007

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

For Exch 2010 对于Exch 2010

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

For Exch 2013 (try this) 对于Exch 2013(尝试此操作)

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

You should be able to just include the Exchange Management snapin before trying the Get-Mailbox command. 在尝试使用Get-Mailbox命令之前,您应该只能够包括Exchange Management管理单元。 Here's how I do that in PowerShell: 这是我在PowerShell中执行的操作:

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

If you are remote from the Exchange Server which your code suggests then you'd have to use something like: 如果您的代码建议您远离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); 

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

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