I’m developing c# application to call Exchange Management Shell Cmdlets. It always comes out with an exception of “The server certificate on the destination computer (208.243.XX.2XX:443) has the following errors:
The SSL certificate is signed by an unknown certificate authority.
The SSL certificate contains a common name (CN) that does not match the hostname. “
But I did write code to accept all certificate, don’t know why still get the error.
My code:
PSCredential credential = new PSCredential("administrator", securePwd);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://208.243.49.20/powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("New-Mailbox");
command.AddParameter("Name", "TestName");
powershell.Commands = command;
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(
delegate { return true; }
);
try
{
runspace.Open();//This is where the exception happens
powershell.Runspace = runspace;
Collection<PSObject> result= powershell.Invoke();
}
WSManConnectionInfoobject has two properties to skip certificate checks.