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();