本文介绍了从 PowerShell 使用 WinRM 连接到远程服务器失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 powershell 代码从我的计算机运行到我计算机上的 vm,但我不断收到此错误:

I am trying to run powershell code from my computer to vm on my computer, but i keep getting this error:

连接到远程服务器失败并显示以下错误消息:WinRM 客户端无法处理该请求.如果认证方案与 Kerberos 不同,或者如果客户端计算机不是加入域,则必须使用 HTTPS 传输或目标机器必须添加到 TrustedHosts 配置中环境.使用 winrm.cmd 配置 TrustedHosts.注意电脑TrustedHosts 列表中的内容可能未经过身份验证.你可以获得更多通过运行以下命令获得有关该信息的信息:winrm help配置.有关详细信息,请参阅 about_Remote_Troubleshooting帮助主题.

我的代码:

  string runasUsername = @"aaa";
    string runasPassword = "aaa";
    SecureString ssRunasPassword = new SecureString();
    foreach (char x in runasPassword)
        ssRunasPassword.AppendChar(x);
    PSCredential credentials = new PSCredential(runasUsername, ssRunasPassword);

    var connInfo = new WSManConnectionInfo(new Uri("http://10.0.5.35/PowerShell"),
        "http://schemas.microsoft.com/powershell/Microsoft.Exchange",credentials);
    connInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

    var runspace = RunspaceFactory.CreateRunspace(connInfo);


    var domainName = "domainName.COM";
    var password = "ActiveDirectoryPassword1234";
    var ssPassword = new SecureString();
    foreach (char c in password)
        ssPassword.AppendChar(c);


    var command = new Command("New-Mailbox");

    command.Parameters.Add("FirstName", firstName);
    command.Parameters.Add("LastName", lastName);
    command.Parameters.Add("Password", ssPassword);
    command.Parameters.Add("ResetPasswordOnNextLogon", false);
    command.Parameters.Add("OrganizationalUnit", "NeumontStudents");

    runspace.Open(); <--//error here
    var pipeline = runspace.CreatePipeline();
    pipeline.Commands.Add(command);


    var results = pipeline.Invoke();

    runspace.Dispose();

我错过了什么?

推荐答案

如果客户端和远程机器不在同一个域,您有以下两种选择之一:

If the client and the remote machine aren't on the same domain, you have one of two options:

  • 使用 HTTPS 作为传输协议
  • 将远程机器添加到客户端的可信主机列表中

为了配置WinRM 要使用 HTTPS,请在两台机器上以管理员身份打开 PowerShell 控制台并运行:

In order to configure WinRM to use HTTPS, open up a PowerShell console as administrator on both machines and run:

winrm quickconfig -transport:https

并在防火墙上打开端口 5986:

and open port 5986 on the firewall:

netsh firewall add portopening TCP 5986 "WinRM over HTTPS"

或者,您可以通过运行以下命令将远程计算机添加为客户端上的可信主机:

Alternatively, you can add the remote machine as trusted host on the client by running:

winrm set winrm/config/client @{TrustedHosts="10.0.5.35"}

这篇关于从 PowerShell 使用 WinRM 连接到远程服务器失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 16:14