繁体   English   中英

Powershell 使用 HttpClient 编写证书脚本

[英]Powershell certificate scripting using HttpClient

我希望使用 powershell 使用 HttpClient 从不同的 API 获取证书信息。 (试图编写一个脚本来查找证书何时过期)。 我对 powershell 很陌生,不知道从哪里开始。 根据研究,我正在尝试下面的代码,但是在使用 [Net.HttpWebRequest] 时,它会在查看 $req.ServicePoint.Certificate 时带回证书 null。 从 [https://github.com/do.net/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()

您可以在 do.net 核心中使用 TcpClient。

$hostname = 'www.google.com'
$ExpirationDays = 90
$request = [System.Net.Sockets.TcpClient]::new($hostname, '443')
$stream = [System.Net.Security.SslStream]::new($request.GetStream())
$stream.AuthenticateAsClient($hostname)
$effectiveDate = $stream.RemoteCertificate.GetEffectiveDateString() -as [datetime]
$expirationDate = $stream.RemoteCertificate.GetExpirationDateString() -as [datetime]
if ($expirationDate -lt [datetime]::UtcNow.AddDays($ExpirationDays)) {
    Write-Host "##vso[task.logissue type=warning] Update certificate for [$hostname]"
    $expiringObjectList += [pscustomobject] @{
        Hostname       = $stream.TargetHostName
        Thumbprint     = $stream.RemoteCertificate.Thumbprint
        Start          = [string]$effectiveDate
        End            = [string]$expirationDate
        ExpirationDays = (New-TimeSpan -Start (Get-Date) -End $expirationDate).Days
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM