Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PowerShell远程命令因未加密的通信量而失败_C#_Powershell_Exchange Server_Powershell 2.0 - Fatal编程技术网

C# PowerShell远程命令因未加密的通信量而失败

C# PowerShell远程命令因未加密的通信量而失败,c#,powershell,exchange-server,powershell-2.0,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 r

以下代码运行:

        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();
异常抛出:

连接到远程服务器失败,错误消息如下: WinRM客户端无法处理该请求。未加密的流量是无效的 当前已在客户端配置中禁用。更换客户机 配置,然后重试该请求。有关更多信息,请参阅 关于远程故障排除帮助主题

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


我在这里尝试了解决方案,但运气不佳。

抱歉,挣扎了很长时间,不断更改可能的组合,最后解决了以下问题:

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();

我也有同样的问题。还应该指出,对于承载PowerShell实例的EXCHANGE_服务器上的虚拟目录,您应该在IIS管理器中将SSL设置配置为“接受”,而不是“需要SSL”,前提是您仍然在服务器上安装了默认自签名证书。再加上“AuthenticationMechanism.Default”设置,我在第行遇到了无数异常:

runspace.Open();
另外,如果您想在本地进行单元测试,您应该在桌面上进行


…或者,如果您没有Windows 8,请尝试以下方法:。

AuthenticationMechanism.Default对我有效,但会导致另一条错误消息

WinRM客户端无法处理该请求。默认身份验证 可在以下条件下与IP地址一起使用: 传输为HTTPS或目标位于TrustedHosts列表中,并且 提供了显式凭据。使用winrm.cmd配置 信任的主人。请注意,TrustedHosts列表中的计算机可能不会 被认证。有关如何设置TrustedHosts运行的详细信息,请参阅 以下命令:winrm help config。有关详细信息,请参阅 关于远程故障排除帮助主题

请注意,EXCHANGE_服务器必须是DNS名称,而不是我使用的IP地址。我还必须在客户端和Exchange服务器上设置AllowUnencrypted配置设置。有关该设置的详细信息,请参阅下面的链接

Basic是一种明文(未加密)身份验证,类似于在网站上获得PopuBox时的身份验证。WS-man的默认设置是在域中使用Kerberos(加密)或在非域计算机之间进行协商。