C# PSRemotingTransportException在C中创建远程PowerShell运行空间时发生异常#
我有以下代码连接到远程计算机:C# PSRemotingTransportException在C中创建远程PowerShell运行空间时发生异常#,c#,powershell,C#,Powershell,我有以下代码连接到远程计算机: var credential = new PSCredential(username, securePassword); var rri = new WSManConnectionInfo(new Uri(uri), schema, credential) { AuthenticationMechanism = AuthenticationMechanism.Kerberos, Pro
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
style,而不是@Donal在评论中所说的Domain\Username
。如果您在尝试启动远程连接和执行命令时遇到这种错误,很可能是您使用了错误的凭据。您可能希望交叉检查您的凭据,使用您用来登录计算机的凭据。请注意,我在这里使用来自环境变量的相同凭据: 如果您使用Microsoft帐户登录计算机,有时使用环境变量可能会产生误导。请改用您的Microsoft凭据。
现在,如果您使用与您的Microsoft帐户相同的凭据,它就可以工作了。
@Donal我已经创建了PowerShell脚本。关键是等效代码在C#上不起作用。您是否尝试过使用username@domainname.com用户名是多少?@Donal我查过了。将用户名更改为您的格式后,会引发相同的异常,但会显示不同的消息:
登录失败:未知用户名或错误密码
。但是,它在PowerShell ISE中工作。它可能正在尝试重定向。尝试将MaximumConnectionRedirectionCount设置为1,即rri.MaximumConnectionRedirectionCount=1;