繁体   English   中英

PSRemotingTransportException在C#中创建远程PowerShell Runspace

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

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

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中收到此异常?

我不知道为什么! 但是我将代码更改为此并且它起作用了。 我刚刚将端口号设置为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,
            };

我也使用了Username@Domain样式,而不是@Donal在评论中说的Domain\\Username

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

暂无
暂无

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

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