使用批处理文件提取证书

Extract a certificate using a Batch file

在Windows中有没有使用批处理文件(或powershell脚本)下载https证书的方法?在 Linux 我可以使用 openssl s_client -connect <URL> ... 我如何在 windows 中执行此操作(不使用第三方 openssl 实现)?

前段时间我写了一个 Test-WebServerSSL PowerShell 脚本来做这件事:Test remote web server SSL certificate. The function utilizes built-in System.Net.WebRequest class and its ServicePoint 属性。

只需 copy/paste 将函数发送到您的 PowerShell 控制台并通过指定远程服务器主机名调用它:

Test-WebServerSSL www.mydomain.com

请注意,您必须只键入不带协议前缀和相对 URL 路径的远程服务器的主机名。

要将证书保存到文件,您可以将输出对象发送到 Set-Content cmdlet:

$SSL = Test-WebserverSSL www.mydomain.com
Set-Content -Path $CertificateFile -Value $SSL.Certificate.RawData -Encoding Byte