在C#中创建远程PowerShell运行空间时出现PSRemotingTransportException

时间:2014-08-25 14:07:06

标签: c# powershell

我有以下代码连接到远程计算机:

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

但它引发了以下异常:

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.

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

作为提示,我在运行此脚本之前也在ISE中收到此异常:

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

另一点是,运行脚本的计算机和远程计算机位于不同的域

为什么我在C#中而不是在PowerShell ISE中获得此异常?

2 个答案:

答案 0 :(得分:2)

我不知道为什么!但是我把代码更改为了这个并且它有效。我只是将端口号码设置为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,
            };

@Donal在评论中说,我还使用Username@Domain样式代替Domain\Username样式。

答案 1 :(得分:0)

我在尝试启动远程连接并执行命令时遇到这种错误,很可能是您使用了错误的凭据。您可能希望交叉检查凭据,使用用于登录计算机的凭据 请注意,我在这里使用来自环境变量的相同凭据: 如果您使用Microsoft帐户登录计算机,有时使用环境变量可能会误导。请改用您的Microsoft凭据 现在,如果您使用与Microsoft帐户相同的凭据,它将起作用。

相关问题