Question

I am trying to run powershell code from my computer to vm on my computer, but i keep getting this error:

Connecting to remote server failed with the following error message : The WinRM client cannot process the request. If the authentication scheme is different from Kerberos, or if the client computer is not joined to a domain, then HTTPS transport must be used or the destination machine must be added to the TrustedHosts configuration setting. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. You can get more information about that by running the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.

my code:

  string runasUsername = @"\aaa";
    string runasPassword = "aaa";
    SecureString ssRunasPassword = new SecureString();
    foreach (char x in runasPassword)
        ssRunasPassword.AppendChar(x);
    PSCredential credentials = new PSCredential(runasUsername, ssRunasPassword);

    var connInfo = new WSManConnectionInfo(new Uri("http://10.0.5.35/PowerShell"),
        "http://schemas.microsoft.com/powershell/Microsoft.Exchange",credentials);
    connInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

    var runspace = RunspaceFactory.CreateRunspace(connInfo);


    var domainName = "domainName.COM";
    var password = "ActiveDirectoryPassword1234";
    var ssPassword = new SecureString();
    foreach (char c in password)
        ssPassword.AppendChar(c);


    var command = new Command("New-Mailbox");

    command.Parameters.Add("FirstName", firstName);
    command.Parameters.Add("LastName", lastName);
    command.Parameters.Add("Password", ssPassword);
    command.Parameters.Add("ResetPasswordOnNextLogon", false);
    command.Parameters.Add("OrganizationalUnit", "NeumontStudents");

    runspace.Open(); <--//error here
    var pipeline = runspace.CreatePipeline();
    pipeline.Commands.Add(command);


    var results = pipeline.Invoke();

    runspace.Dispose();

What am I missing?

Was it helpful?

Solution

If the client and the remote machine aren't on the same domain, you have one of two options:

  • use HTTPS as a transport protocol
  • add the remote machine to the list of trusted hosts on the client

In order to configure WinRM to use HTTPS, open up a PowerShell console as administrator on both machines and run:

winrm quickconfig -transport:https

and open port 5986 on the firewall:

netsh firewall add portopening TCP 5986 "WinRM over HTTPS"

Alternatively, you can add the remote machine as trusted host on the client by running:

winrm set winrm/config/client @{TrustedHosts="10.0.5.35"}

OTHER TIPS

have you enabled winrm on both machines? try running winrm quickconfig on each machine to ensure remote connectivity is enabled.

Setting this up on a new client, I had to:

  1. Run PowerShell as administrator

  2. Enable WinRM by running this command and answering yes to the prompt:

    winrm quickconfig
    
  3. Add the host to my trusted hosts (where 1.2.3.4 is the host's IP address):

    Set-Item wsman:localhost\client\trustedhosts -value 1.2.3.4
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top