【问题标题】:Powershell certificate scripting using HttpClient使用 HttpClient 编写 Powershell 证书脚本
【发布时间】: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()

【问题讨论】:

标签: .net powershell


【解决方案1】:

您可以在 dotnet core 中使用 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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-19
    • 2016-02-07
    • 1970-01-01
    • 2019-06-24
    • 2018-06-08
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    相关资源
    最近更新 更多