0

I'm trying to connect to powershell using C#. But while connectting I'm getting the following error when trying to open the runspace.

"Connecting to remote server failed with the following error message : The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated."

Below is my code

string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
            PSCredential remoteCredential = new PSCredential("Username", StringToSecureString("Password"));
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "Server IP", 5985, "/wsman", shellUri, remoteCredential);

            Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);

            runspace.Open();

            Pipeline pipeline = runspace.CreatePipeline();

            Command newMailBox = new Command("New-Mailbox");

            newMailBox.Parameters.Add("Name", "TestName1");
            newMailBox.Parameters.Add("Alias", "TestName1");
            newMailBox.Parameters.Add("database", "Mailbox Database 1406738839");
            newMailBox.Parameters.Add("DisplayName", "TestName1");
            newMailBox.Parameters.Add("UserPrincipalName", "TestName1@test.com");
            newMailBox.Parameters.Add("OrganizationalUnit", "ou=myorg,dc=ad,dc=lab");
            newMailBox.Parameters.Add("FirstName", "TestName1");

            pipeline.Commands.Add(newMailBox);

            Collection<PSObject> result = pipeline.Invoke();
  • http://stackoverflow.com/a/6667923/478656 – TessellatingHeckler Nov 25 '16 at 06:28
  • Previous error is gone. Now getting new error. "Connecting to remote server failed with the following error message : The WinRM client cannot complete the operation within the time specified. Check if the machine name is valid and is reachable over the network and firewall exception for Windows Remote Management service is enabled." – Hiren Khatri Nov 25 '16 at 07:06

0 Answers0