【问题标题】:Powershell script to find currently bound expiring certificates in IIS remotely用于在 IIS 中远程查找当前绑定的过期证书的 Powershell 脚本
【发布时间】:2017-10-18 20:52:03
【问题描述】:

我目前正在编写一个脚本,一旦绑定在我的 Web 服务器的 IIS 中的证书接近到期日期,它将发送一封电子邮件。我确实有通过电子邮件发送的脚本。我只需要知道如何比较商店查询中可用的证书与当前使用的证书。现在,这就是我所拥有的:

$Date= (Get-Date)
$SMTPServer = "smtp.test.com" 
$From = "testmail@noreply.com"

Import-Module WebAdministration

$Servers = @("WEBSERVER1", "WEBSERVER2")

$certificates = foreach($server in $Servers){
    Invoke-Command -ComputerName $server -ScriptBlock { $CertAll = Get-ChildItem -Path Cert:\LocalMachine\My }
    Invoke-Command -ComputerName $server -ScriptBlock { $CertInUse = Get-ChildItem -Path IIS:\SslBindings }
    Invoke-Command -ComputerName $server -ScriptBlock { $CertSame = Compare-Object -ReferenceObject $CertAll -DifferenceObject $CertInUse -Property Thumbprint -IncludeEqual -ExcludeDifferent }

    Invoke-Command -ComputerName $server -ScriptBlock { $cert = $CertSame | ForEach {Get-ChildItem -Path Cert:\LocalMachine\My\$($_.thumbprint)} | 
  Select-Object Subject, DaysUntilExpired, NotAfter, @{n='ExpireInDays';e={($_.notafter - ($Date)).Days}}}
}

    $certificates | Sort DisplayName

任何帮助和建议将不胜感激。谢谢!

【问题讨论】:

    标签: powershell powershell-remoting windows-scripting


    【解决方案1】:

    上面的脚本永远不会起作用,因为您在同一台计算机的不同会话中创建变量。

    你可以通过两种方式做到这一点。

    1. 创建一个以目标服务器为目标的会话对象并重复使用它。这样您就可以在后续的Invoke-command 执行中获取会话中定义的变量。

    2. 不创建会话对象,而是在单个 Invoke-Command 中执行远程服务器上的所有内容。

    例子:-

    Invoke-command -computerName $Server {
        $CertAll = ....
        $CertInUse = ....
        $CertSame = ....
        $cert = $CertSame | ForEach ..... |
        Select-Object Subject, DaysUntilExpired .....
    
    }
    

    如果您在确定证书过期日期后对远程服务器没有任何进一步的操作,我建议使用第二个选项。

    【讨论】:

    • @Didge 添加示例
    • 我现在明白了。对不起先生的困惑。但我想我无法访问IIS:\SslBindings。我总是收到错误提示 Cannot find drive. A drive with the name 'IIS' does not exist. 这是我认为出现错误的部分:Get-ChildItem -Path IIS:\SslBindings
    • @Didge 您需要在访问“IIS:\”之前导入“WebAdministration”模块
    • @PRASOON 是的,我确实有。不过,我仍然收到错误消息。我是否必须更改 IIS 中的某些内容?另外,我以管理员身份运行 ISE。 :(
    【解决方案2】:

    @PRASOON 我已经设法远程检查我的证书。我尝试使用在谷歌中找到的不同参考资料。无论如何,这是脚本。

    $Date = Get-Date
    $servers = Get-Content C:\servers.txt
    
    $cert = Foreach ($server in $servers) {
        Invoke-Command -ComputerName $server -ScriptBlock{
            Import-Module WebAdministration; Get-ChildItem -Path IIS:SslBindings | ForEach-Object -Process{
                if ($_.Sites)
                    {
                        $certificate = Get-ChildItem -Path CERT:LocalMachine\My |
                            Where-Object -Property Thumbprint -EQ -Value $_.Thumbprint
    
                        [PSCustomObject]@{
                            Sites = $_.Sites.Value
                            DnsNameList = $certificate.DnsNameList
                            NotAfter = $certificate.NotAfter
                            ExpireInDays = ($certificate.NotAfter - (Get-Date)).Days}
                    }
                } 
            }
        } 
    
    $cert | Select PSComputerName, DnsNameList, NotAfter, ExpireInDays | Where-Object {$_.ExpireInDays -lt 30} | Out-File C:\results.txt
    

    所以基本上,这将显示将完全到期或从现在起 30 天内到期的证书。我仍在为此工作,因为我要做的是在脚本检测到将从当前日期起 30 天到期的证书时发送电子邮件并发送电子邮件通知。我将在另一篇文章中询问我对此的担忧。

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 2019-06-24
      • 2018-11-07
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多