【发布时间】:2022-04-28 23:25:51
【问题描述】:
我希望通过 HttpClient 使用 powershell 从不同的 API 获取证书信息。 (尝试编写一个脚本来查找证书何时过期)。我对powershell很陌生,不知道从哪里开始。根据研究,我正在尝试下面的代码,但是当使用 [Net.HttpWebRequest] 时,它会在查看 $req.ServicePoint.Certificate 时为证书返回一个空值。从 [https://github.com/dotnet/runtime/issues/29301] 这个资源看起来 HttpWebRequest 已经过时了。关于使用 powershell 检索证书信息有什么建议吗??
$timeoutMs = 10000
$sites = @("https://testsite1.com/")
Write-Host Checking $sites -f Green
$req = [Net.HttpWebRequest]::Create($sites)
$expDate = $req.ServicePoint.Certificate.GetExpirationDateString()
【问题讨论】:
-
create() 方法只是创建一个 HttpWebRequest 的实例。您需要使用其中一种可用的方法(例如 GetResponse() 等)实际执行请求。此外,create() 方法只接受一个字符串,而不是一个值数组 (docs.microsoft.com/en-us/dotnet/api/…)。另外,根据 HttpWebRequest 类的最新文档,您应该使用 System.Net.Http.HttpClient 而不是 HttpWebRequest:docs.microsoft.com/en-us/dotnet/api/…
标签: .net powershell