Powershell版本之间System.Net.HttpWebRequest的更改?

问题描述

将Powershell脚本从PS5转换为PS7时遇到麻烦。

我的目标是获取有关远程服务器证书的信息。

此IP可以是带有证书的任何内容

    $url = "https://192.168.1.1"
    [Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
    $req = [Net.HttpWebRequest]::Create($url)
    $req.GetResponse() 
    [datetime]$expiration = 
    [System.DateTime]::Parse($req.ServicePoint.Certificate.GetExpirationDateString())
    Write-Output $expiration

在Powershell 5中运行时,此脚本成功完成,并显示以下输出

IsMutuallyAuthenticated : False
Cookies                 : {}
Headers                 : {Accept-Ranges,Content-Length,Content-Type,Date...}
SupportsHeaders         : True
ContentLength           : 98540
ContentEncoding         : 
ContentType             : text/html
CharacterSet            : ISO-8859-1
Server                  : Microsoft-IIS/10.0

...

Date        : 7/7/2020 12:00:00 AM
...

在Powershell 7中运行时,此脚本在$req.GetResponse上出错。

MethodInvocationException: Exception calling "GetResponse" with "0" argument(s): "The SSL connection Could not be established,see inner exception. There is no Runspace available to run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script block you attempted to invoke was:  $true "

有什么想法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)