使用PowerShell通过WinRM连接远程服务器失败

27

我正在尝试从我的计算机运行PowerShell代码到我的计算机上的vm,但是我不断收到以下错误:

连接到远程服务器失败,显示以下错误消息: WinRM客户端无法处理请求。如果身份验证方案与Kerberos不同,或者客户端计算机未加入域,则必须使用HTTPS传输或将目标计算机添加到TrustedHosts配置设置中。使用winrm.cmd配置TrustedHosts。请注意,TrustedHosts列表中的计算机可能未经过身份验证。您可以通过运行以下命令获取有关此问题的更多信息:winrm help config。有关详细信息,请参阅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();

我缺少什么?


1
你尝试过检查错误信息中提到的事项吗? - alex.b
3个回答

35

如果客户端和远程机器不在同一域中,则有两个选择:

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

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

winrm quickconfig -transport:https

在防火墙上打开端口5986:

netsh firewall add portopening TCP 5986 "WinRM over HTTPS"

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

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

2
是的,我明白了:“WinRM已经设置好在此计算机上接收请求。 WinRM已经在此计算机上设置好远程管理。” - woolford
有一种使用自签名证书的HTTPS方式:http://www.jayway.com/2011/11/21/winrm-w-self-signed-certificate-in-4-steps/ - skolima
2
好的,如果你去掉单引号,winrm命令才能正常工作。(对我来说,这仍然没有解决问题)。 - MGOwen
1
当我运行winrm set winrm/config/client @{TrustedHosts="<my_host_ip>"}时,出现了“错误:命令行使用无效”。 - tolache
1
我也遇到了“无效使用”错误,加上单引号后问题得以解决:winrm set winrm/config/client '@{TrustedHosts="10.0.5.35"}' - Iomm1
显示剩余2条评论

4

在为新客户设置这个功能时,我需要做以下操作:

  1. Run PowerShell as administrator

  2. Enable WinRM by running this command and answering yes to the prompt:

    winrm quickconfig
    
  3. Add the host to my trusted hosts (where 1.2.3.4 is the host's IP address):

    Set-Item wsman:localhost\client\trustedhosts -value 1.2.3.4
    

通过运行以下命令并在提示中回答“是”,启用WinRM: winrm quickconfig请注意,您必须在客户端和服务器计算机上都运行此命令,否则下一个命令将失败。 - Msprg

2

您是否在两台计算机上启用了WinRM? 尝试在每台计算机上运行winrm quickconfig以确保启用了远程连接。


1
是的,我明白了:“WinRM已经设置好在此计算机上接收请求。 WinRM已经在此计算机上设置好远程管理。” - woolford
尝试使用CredSSP身份验证。请查看我在这里的答案中的步骤: https://dev59.com/YG_Xa4cB1Zd3GeqP1nwb#15336790 - Musaab Al-Okaidi
CredSSP用于允许远程计算机在连接到其他服务(即第二跳)时传递客户端凭据。问题在于此情况下客户端和服务器不在同一个域中。 - Enrico Campidoglio
我理解这一点,但我认为这将解决跨域身份验证问题,因为标准身份验证无法正常工作。 - Musaab Al-Okaidi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接