C# 从 PowerShell 使用 WinRM 连接到远程服务器失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16010091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting to remote server failed using WinRM from PowerShell
提问by woolford
I am trying to run powershell code from my computer to vm on my computer, but i keep getting this error:
我正在尝试将 powershell 代码从我的计算机运行到我计算机上的 vm,但我不断收到此错误:
Connecting to remote server failed with the following error message : The WinRM client cannot process the request. If the authentication scheme is different from Kerberos, or if the client computer is not joined to a domain, then HTTPS transport must be used or the destination machine must be added to the TrustedHosts configuration setting. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. You can get more information about that by running the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.
连接到远程服务器失败并显示以下错误消息:WinRM 客户端无法处理请求。如果身份验证方案与 Kerberos 不同,或者如果客户端计算机未加入域,则必须使用 HTTPS 传输或必须将目标计算机添加到 TrustedHosts 配置设置。使用 winrm.cmd 配置 TrustedHosts。请注意,TrustedHosts 列表中的计算机可能未经过身份验证。您可以通过运行以下命令获取更多信息:winrm help config。有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。
my code:
我的代码:
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();
What am I missing?
我错过了什么?
采纳答案by Enrico Campidoglio
If the client and the remote machine aren't on the same domain, you have one of two options:
如果客户端和远程机器不在同一个域中,您有以下两种选择之一:
- use HTTPS as a transport protocol
- add the remote machine to the list of trusted hosts on the client
- 使用 HTTPS 作为传输协议
- 将远程机器添加到客户端的可信主机列表中
In order to configure WinRMto use HTTPS, open up a PowerShell console as administratoron both machines and run:
为了将WinRM 配置为使用 HTTPS,请在两台计算机上以管理员身份打开 PowerShell 控制台并运行:
winrm quickconfig -transport:https
and open port 5986 on the firewall:
并在防火墙上打开端口 5986:
netsh firewall add portopening TCP 5986 "WinRM over HTTPS"
Alternatively, you can add the remote machine as trusted host on the clientby running:
或者,您可以通过运行以下命令将远程计算机添加为客户端上的受信任主机:
winrm set winrm/config/client @{TrustedHosts="10.0.5.35"}
回答by Musaab Al-Okaidi
have you enabled winrm on both machines?
try running winrm quickconfigon each machine to ensure remote connectivity is enabled.
你在两台机器上都启用了 winrm 吗?尝试winrm quickconfig在每台机器上运行以确保启用远程连接。

