【问题标题】:Modifying SSL cert check Powershell script to loop through multiple sites修改 SSL 证书检查 Powershell 脚本以循环访问多个站点
【发布时间】:2015-02-09 20:08:23
【问题描述】:

我是 Powershell 新手,但我正在尝试编写一个脚本来检查多个远程网站的 SSL 证书到期日期。

我发现这个脚本 (http://www.zerrouki.com/checkssl/) 可以满足我的需求,但仅适用于单个站点。 我正在尝试对其进行修改以允许多个站点/检查,但是这样做时出现错误。我已经从脚本中删除了所有电子邮件功能,因为我将使用另一个工具来提醒即将到期的证书。我已经硬编码了要检查的 URL。

 <#
    Modified from Fabrice ZERROUKI - fabricezerrouki@hotmail.com Check-SSL.ps1
#>
$WebsiteURLs= @("URL1.com","URL2.com","URL3.com")
$WebsitePort=443
$CommonName=$WebsiteURL
$Threshold=120

foreach ($WebsiteURL in $WebsiteURLs){
Try{
    $Conn = New-Object System.Net.Sockets.TcpClient($WebsiteURL,$WebsitePort) 

    Try {
        $Stream = New-Object System.Net.Security.SslStream($Conn.GetStream())
        $Stream.AuthenticateAsClient($CommonName) 

        $Cert = $Stream.Get_RemoteCertificate()

        $ValidTo = [datetime]::Parse($Cert.GetExpirationDatestring())

        Write-Host "`nConnection Successfull" -ForegroundColor DarkGreen
        Write-Host "Website: $WebsiteURL"

        $ValidDays = $($ValidTo - [datetime]::Now).Days

        if ($ValidDays -lt $Threshold)
        {
        Write-Host "`nStatus: Warning (Expires in $ValidDays days)" -ForegroundColor Yellow
        Write-Host "CertExpiration: $ValidTo`n" -ForegroundColor Yellow

        }
        else
        {
        Write-Host "`nStatus: OK" -ForegroundColor DarkGreen
        Write-Host "CertExpiration: $ValidTo`n" -ForegroundColor DarkGreen
        }
    }
    Catch { Throw $_ }
    Finally { $Conn.close() }
    }
    Catch {
            Write-Host "`nError occurred connecting to $($WebsiteURL)" -ForegroundColor Yellow
            Write-Host "Website: $WebsiteURL"
            Write-Host "Status:" $_.exception.innerexception.message -ForegroundColor Yellow
            Write-Host ""

}
}

当我运行此程序时(在 $WebsiteURLs 变量中使用有效站点),每个站点都会返回:状态:身份验证失败,因为远程方已关闭传输流。

如果我只将一个站点放在 $WebsiteURLs 变量中并删除它运行正常的 foreach 函数。

知道我可以做些什么来使这个循环遍历变量中的每个站点吗?

【问题讨论】:

    标签: powershell ssl


    【解决方案1】:

    问题出在这里:

    $WebsiteURLs= @("URL1.com","URL2.com","URL3.com")
    $WebsitePort=443
    $CommonName=$WebsiteURL
    

    当您调用$Stream.AuthenticateAsClient($CommonName) 时,它不起作用,因为$CommonName=$WebsiteURL$commonName 设置为null。当您删除循环时,我假设您像我一样将$WebsiteURLs 更改为$WebsiteURL,这样您就有了一个可以分配$CommonName 的值。

    如果您将 $CommonName 的声明移动到您的循环中,它会起作用。

    $WebsiteURLs= @("URL1.com","URL2.com","URL3.com")
    $WebsitePort=443
    $Threshold=120
    
    foreach ($WebsiteURL in $WebsiteURLs){
    $CommonName=$WebsiteURL
    Try{
        $Conn = New-Object System.Net.Sockets.TcpClient($WebsiteURL,$WebsitePort) 
    
        Try {
            $Stream = New-Object System.Net.Security.SslStream($Conn.GetStream())
            $Stream.AuthenticateAsClient($CommonName) 
    
            $Cert = $Stream.Get_RemoteCertificate()
    
            $ValidTo = [datetime]::Parse($Cert.GetExpirationDatestring())
    
            Write-Host "`nConnection Successfull" -ForegroundColor DarkGreen
            Write-Host "Website: $WebsiteURL"
    
            $ValidDays = $($ValidTo - [datetime]::Now).Days
    
            if ($ValidDays -lt $Threshold)
            {
            Write-Host "`nStatus: Warning (Expires in $ValidDays days)" -ForegroundColor Yellow
            Write-Host "CertExpiration: $ValidTo`n" -ForegroundColor Yellow
    
            }
            else
            {
            Write-Host "`nStatus: OK" -ForegroundColor DarkGreen
            Write-Host "CertExpiration: $ValidTo`n" -ForegroundColor DarkGreen
            }
        }
        Catch { Throw $_ }
        Finally { $Conn.close() }
        }
        Catch {
                Write-Host "`nError occurred connecting to $($WebsiteURL)" -ForegroundColor Yellow
                Write-Host "Website: $WebsiteURL"
                Write-Host "Status:" $_.exception.innerexception.message -ForegroundColor Yellow
                Write-Host ""
    
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-15
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 2014-10-04
      • 2019-04-22
      • 1970-01-01
      相关资源
      最近更新 更多