簡體   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