我正在开发c#应用程序来调用Exchange命令行管理程序Cmdlet。它总是会出现一个例外:“目标计算机上的服务器证书(208.243.XX.2XX:443)出现以下错误:
SSL证书由未知证书颁发机构签署。
SSL证书包含通用名称(CN)与主机名称不匹配。“如何忽略SSL证书由未知的证书颁发机构问题签名?
但我确实编写了接受所有证书的代码,不知道为什么还是得到错误。
我的代码:
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();
}
感谢您的快速回复。在创建运行空间实例之前,我移动了ServicePointManager,发生了相同的错误。我改变返回true返回false,同样的错误。我不确定是否从单独的进程执行powershell,但似乎ServicePointManager在此过程中没有任何关系。如果它是一个独立的过程,那该怎么办?提前致谢。 –
你有没有试过把它作为第一个命令? –
我越来越相信这是因为powershell运行在一个单独的过程中。您需要让powershell脚本执行与ServicePointManager配置步骤等效的操作。 –