简体   繁体   English

PSRemotingTransportException在C#中创建远程PowerShell Runspace

[英]PSRemotingTransportException in creating a Remote PowerShell Runspace in C#

I have the following code to connect to a remote computer: 我有以下代码可连接到远程计算机:

var credential = new PSCredential(username, securePassword);
        var rri = new WSManConnectionInfo(new Uri(uri), schema, credential)
        {
            AuthenticationMechanism = AuthenticationMechanism.Kerberos,
            ProxyAuthentication = AuthenticationMechanism.Negotiate
        };

        var remoteRunspace = RunspaceFactory.CreateRunspace(rri);
        remoteRunspace.Open();

But it's throwing the following exception: 但这引发了以下异常:

System.Management.Automation.Remoting.PSRemotingTransportException was unhandled by user code
  HResult=-2146233087
  Message=Connecting to remote server sw-spdev02 failed with the following error message : WinRM cannot process the request. The following error with errorcode 0x80090311 occurred while using Kerberos authentication: There are currently no logon servers available to service the logon request.  
 Possible causes are:
  -The user name or password specified are invalid.
  -Kerberos is used when no authentication method and no user name are specified.
  -Kerberos accepts domain user names, but not local user names.
  -The Service Principal Name (SPN) for the remote computer name and port does not exist.
  -The client and remote computers are in different domains and there is no trust between the two domains.
 After checking for the above issues, try the following:
  -Check the Event Viewer for events related to authentication.
  -Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or use HTTPS transport.
 Note that computers in the TrustedHosts list might not be authenticated.

The equivalent code in PowerShell ISE is working properly: PowerShell ISE的等效代码正常运行:

$cred = new-object -typename System.Management.Automation.PSCredential `-argumentlist 'domain\user', ('password' | ConvertTo-SecureString -AsPlainText -Force)
$session = New-PSSession http://servername -Credential $cred

As a hint, I was getting this exception in ISE too before I ran this script: 作为提示,在运行此脚本之前,我也在ISE也遇到了此异常:

Set-Item -Path WSMan:\localhost\Client\TrustedHosts -Value http://servername

Another point is that, the computer which the script is running on, and the remote computer are in different domains . 另一点是,运行脚本的计算机和远程计算机位于不同的域中

Why am I getting this exception in C# and not in PowerShell ISE? 为什么我在C#中而不在PowerShell ISE中收到此异常?

I don't know why! 我不知道为什么! But I changed the code to this and it worked. 但是我将代码更改为此并且它起作用了。 I've just set the port number to 5985: 我刚刚将端口号设置为5985:

var rri = new WSManConnectionInfo(false, "sw-spdev02.mahanair.aero", 5985, "wsman",
                "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", credential)
            {
                ProxyAuthentication = AuthenticationMechanism.Negotiate,
                AuthenticationMechanism = AuthenticationMechanism.Default,
            };

Also I've used Username@Domain style instead of Domain\\Username as @Donal said in the comments. 我也使用了Username@Domain样式,而不是@Donal在评论中说的Domain\\Username

I you get that kind of an error when trying to initiate a remote connection and execute commands , most probably you are using the wrong credentials . 我在尝试启动远程连接并执行命令时遇到这种错误,很可能是您使用了错误的凭据。 You might want to cross check your credentials , use the credentials that you use to login to the machine. 您可能要交叉检查您的凭据,使用您用于登录到计算机的凭据。
Notice that am using the same credentials from the environment variables here: 请注意,这里使用的是来自环境变量的相同凭据: Sometimes using the environment variables might mislead if your using your Microsoft account to login to your machine. 如果您使用Microsoft帐户登录到计算机,则有时使用环境变量可能会误导您。 Use your Microsoft credentials instead. 请改用您的Microsoft凭据。
Now it works if you use the same credentials as your Microsoft Account. 现在,如果您使用与Microsoft帐户相同的凭据,则可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM