PSRemotingTransportException in creating a Remote PowerShell Runspace in C#

0

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:

$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:

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
asked on Stack Overflow Aug 25, 2014 by mehrandvd • edited Aug 25, 2014 by mehrandvd

2 Answers

2

I don't know why! But I changed the code to this and it worked. I've just set the port number to 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.

answered on Stack Overflow Aug 26, 2014 by mehrandvd
0

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. Use your Microsoft credentials instead.
Now it works if you use the same credentials as your Microsoft Account.

answered on Stack Overflow Mar 18, 2016 by big kev • edited Mar 18, 2016 by bummi

User contributions licensed under CC BY-SA 3.0