Powershell 在 URL 中找到带有上下文的网络服务器证书过期

Powershell find webserver certificate expiration with context in URL

需要有关 Powershell 的帮助。 我们需要使用 powershell 查找服务器证书过期时间。这些是 weblogic 控制台 Urls。 URLs 具有上下文和端口,如 https://server:7020/context 。如果我在没有上下文的情况下浏览 URL,我会收到错误消息 -

Error 404--Not Found
From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1:
10.4.5 404 Not Found

我试过以下代码 -

Try{
$Conn = New-Object 

System.Net.Sockets.TcpClient($WebsiteURL,$WebsitePort)

Try {
$Stream = New-Object 

System.Net.Security.SslStream($Conn.GetStream(),$false, {

param($sender, $certificate, $chain, $sslPolicyErrors)
return $true
})
$Stream.AuthenticateAsClient($CommonName) 

如果我在没有上下文的情况下尝试服务器,它会给出以下错误 -

A call to SSPI failed, see inner exception.

在 powershell 中查询的命令和选项是什么? 感谢任何帮助。

使用 Get-RemoteSslCertificate from jstangroome 您可以简单地 运行 以下 return 到期。

(Get-RemoteSslCertificate -ComputerName server -Port 7020).NotAfter

Get-RemoteSslCertificate函数:

function Get-RemoteSslCertificate {
    # Author: jstangroome https://gist.github.com/jstangroome/5945820
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string]
        $ComputerName,
    
        [int]
        $Port = 443
    )
    
    $Certificate = $null
    $TcpClient = New-Object -TypeName System.Net.Sockets.TcpClient
    try {
    
        $TcpClient.Connect($ComputerName, $Port)
        $TcpStream = $TcpClient.GetStream()
    
        $Callback = { param($sender, $cert, $chain, $errors) return $true }
    
        $SslStream = New-Object -TypeName System.Net.Security.SslStream -ArgumentList @($TcpStream, $true, $Callback)
        try {
    
            $SslStream.AuthenticateAsClient('')
            $Certificate = $SslStream.RemoteCertificate
    
        } finally {
            $SslStream.Dispose()
        }
    
    } finally {
        $TcpClient.Dispose()
    }
    
    if ($Certificate) {
        if ($Certificate -isnot [System.Security.Cryptography.X509Certificates.X509Certificate2]) {
            $Certificate = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList $Certificate
        }
    
        Write-Output $Certificate
    }
}