c# - 由于未加密的流量,PowerShell 远程命令失败

标签 c# powershell exchange-server powershell-2.0

运行以下代码:

        SecureString password = new SecureString();
        string runasUsername = "USERNAME";
        string runasPassword = "PASSWORD";

        string liveIdconnectionUri = "http://EXCHANGE_SERVER/PowerShell";

        foreach (char x in runasPassword)
        {
            password.AppendChar(x);
        }

        PSCredential credential = new PSCredential(runasUsername, password);

        // Set the connection Info
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
        credential);

        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; //AuthenticationMechanism.Default;

        // create a runspace on a remote path
        // the returned instance must be of type RemoteRunspace

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

        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();

        command.AddCommand("Enable-Mailbox");
        command.AddParameter("Identity", "first.last");
        command.AddParameter("Alias", "Fist Last");

        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
            var result = powershell.Invoke();
        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.Message);
        }
        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;
        }

        Console.WriteLine("done");
        Console.Read();

抛出异常:

Connecting to remote server failed with the following error message : The WinRM client cannot process the request. Unencrypted traffic is currently disabled in the client configuration. Change the client configuration and try the request again. For more information, see the about_Remote_Troubleshooting Help topic.

我已经设置了基本身份验证,允许未加密的流量。

我在这里尝试了解决方案 powershell v2 remoting - How do you enable unencrypted traffic ,没有运气。

最佳答案

抱歉,挣扎了很长时间,不断改变可能的组合,最后用这个:

AuthenticationMechanism 应该是AuthenticationMechanism.Default,而不是AuthenticationMechanism.Basic(这很奇怪)。

最终的工作版本是:

        SecureString password = new SecureString();
        string runasUsername = "USERNAME";
        string runasPassword = "PASSWORD";

        string liveIdconnectionUri = "http://EXCHANGE_SERVER/PowerShell";

        foreach (char x in runasPassword)
        {
            password.AppendChar(x);
        }

        PSCredential credential = new PSCredential(runasUsername, password);

        // Set the connection Info
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
        credential);

        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default; //AuthenticationMechanism.Default;

        // create a runspace on a remote path
        // the returned instance must be of type RemoteRunspace

        Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);

        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();

        command.AddCommand("Enable-Mailbox");
        command.AddParameter("Identity", "MAIL_USER_ID_HERE");

        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
            var result = powershell.Invoke();
            if (result.Count > 0)
                Console.WriteLine("sucessful!");
            else
                Console.WriteLine("failed!");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        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;
        }

        Console.WriteLine("done");
        Console.Read();

关于c# - 由于未加密的流量,PowerShell 远程命令失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15058209/

相关文章:

c# - INotifyPropertyChanged 不会通过 View 模型

PowerShell 模拟到 "dir/a:d"(Win) 或 "ls -d */"(Bash)

powershell - 如果行不以 : with Powershell 开头,则删除换行符

C#/CIL : type of native int

c# - 清除空MVC部分 View 的干净方法

Javax.naming.* 端口到 Android?

c# - 是否可以对 Mailkit 和 Exchange 使用默认网络凭据?

c# - session 预约是如何连接的以及如何使用此连接?

c# - Datagrid:如何获取 SelectedItem 的 CurrentCell?

PowerShell:如何使用 WMI 在远程计算机上运行 powershell 脚本