【问题标题】:How can I use Powershell to find when an SSL certificate expires for ONLY IIS for a list of servers from OU?如何使用 Powershell 查找仅 IIS 的 SSL 证书何时过期以获取来自 OU 的服务器列表?
【发布时间】:2021-11-19 22:17:05
【问题描述】:

我有这部分代码,如果我只能让脚本只回复存在的主题(表示 IIS 证书),那么我可以完成......(我有 OU 枚举和 Invoke部分,以及用于在任务中安排的文件的电子邮件): [注意:我将到期时间设置为 500 天,因此我可以稍后使用脚本来查找特定的到期时间] [注 2:$day 在我的 $profile 中设置为 '$day = Get-Date -Format yyyyMMdd']

    $serverlist = $serverListpath.Name
    foreach($server in $serverlist){
        if($server -like '#*')
        {
            continue
        }
    
    $threshold = 500   #Number of days to look for expiring certificates
    $deadline = (Get-Date).AddDays($threshold)   #Set deadline date
    $p = ($c++/$server.count) * 100
     Write-Progress -Activity "Checking $._" -Status "$p % completed" -PercentComplete $p;
     if(Test-Connection -ComputerName $server -Count 2 -Quiet){
     #$server = "KnownIISServerHostname" #<-- to test with a hostname
    Invoke-Command -Verbose -ComputerName $server { Dir Cert:\LocalMachine\My } |`
foreach {
    If ($_.NotAfter -le $deadline) { 
$_ | Select *| select PSComputerName, Subject, NotAfter, @{Label="Expires In (Days)";Expression={($_.NotAfter - (Get-Date)).Days}} }
    }|`
select PSComputerName,Subject, NotAfter, @{Label="Expires In (Days)";Expression={($_.NotAfter - (Get-Date)).Days}} |`
    export-csv -Force -Append -Encoding ASCII -NoTypeInformation .\output\$day-ExpiringIISSSLCerts.csv
    }
    }

那么我在哪里调整它以获得只有现有“主题”字段的回复;不获取空主题提交的回复(即 RDP 证书)

【问题讨论】:

    标签: powershell ssl-certificate iis-10


    【解决方案1】:

    尝试使用这个:

    Import-Module WebAdministration
    $CertAll=Get-ChildItem -Path Cert:\LocalMachine\My
    $CertInUse=Get-Childitem -Path IIS:\SslBindings
    $CertSame=Compare-Object -ReferenceObject $CertAll -DifferenceObject $CertInUse -Property ThumbPrint -IncludeEqual -ExcludeDifferent
    $CertSame | foreach{Get-Childitem –path Cert:\LocalMachine\My\$($_.thumbprint)} | Select-Object -Property Subject, @{n=’ExpireInDays’;e={($_.notafter – (Get-Date)).Days}}
    

    【讨论】:

    • 你的很好:不过,在“调用命令”期间运行“-RunAsAdministrator”时实际上不需要导入,因此我们不需要第 1 行的“导入模块 WebAdministration”。现在在 '$CertSame' 'GCI' 中获得正确的过滤器语法,我们很成功! :)
    • 我在 $CertInUse 上不断收到错误 - 这很有效 -: '$CertInUse= Get-ChildItem -Path IIS:SSLBindings'
    • 啊,我们开始了:'$CertInUse= (GCI IIS:SSLBindings)'
    • 我在最后添加了“-First 1”,得到了我需要的东西
    【解决方案2】:

    由于 IIS 证书是您关注的范围,我建议您使用 IIS PowerShell 模块来确保您只选择 IIS 实际使用的证书。

    以下内容应使用 HTTPS(SSL) 提取附加到站点的证书。我目前在单个 IIS 服务器上没有多个站点进行测试,但理论上这应该找到所有站点,而不仅仅是“默认网站”。

    $serverlist = $serverListpath.Name
    foreach($server in $serverlist){
        if($server -like '#*')
        {
            continue
        }
    
    $threshold = 500   #Number of days to look for expiring certificates
    $deadline = (Get-Date).AddDays($threshold)   #Set deadline date
    $p = ($c++/$server.count) * 100
     Write-Progress -Activity "Checking $._" -Status "$p % completed" -PercentComplete $p;
     if(Test-Connection -ComputerName $server -Count 2 -Quiet){
     #$server = "KnownIISServerHostname" #<-- to test with a hostname
     #Pull certificates from existing IIS bindings
     $certificates = Invoke-Command -Verbose -ComputerName $server { 
        Import-Module IISAdministration
        $sitebindings = Get-IISSite | foreach { Get-IISSiteBinding -Protocol HTTPS -Name $_ }
        $thumbprints = $sitebindings.Attributes | where {$_.Name -match "certificateHash"} | Select-Object -ExpandProperty Value
        $thumbprints | foreach {dir Cert:\LocalMachine\My\$_}
        }
    $certificates |`
    foreach {
        If ($_.NotAfter -le $deadline) { 
        $_ | Select *| select PSComputerName, Subject, NotAfter, @{Label="Expires In (Days)";Expression={($_.NotAfter - (Get-Date)).Days}} }
    }|`
    select PSComputerName,Subject, NotAfter, @{Label="Expires In (Days)";Expression={($_.NotAfter - (Get-Date)).Days}} |`
    export-csv -Force -Append -Encoding ASCII -NoTypeInformation .\output\$day-ExpiringIISSSLCerts.csv
    }
    }
    

    【讨论】:

    • 我们不能在每个 IIS 服务器上完全“安装-WindowsFeature web-mgmt-console”,这是加载“IISAdministration”所需要做的...但是谢谢...
    • @PatrickBurwell 确实如此。每个人都有自己的情况,我可以理解不是在每台服务器上启用所有功能。很高兴您能够找到有效的解决方案。
    【解决方案3】:

    #完成本地运行脚本。在 Foreach 调用命令中调用它。

    $CertAll=GCI -Path Cert:\LocalMachine\My
    $CertInUse= (GCI IIS:SSLBindings)
    $CertSame=Compare-Object -ReferenceObject $CertAll -DifferenceObject $CertInUse -Property ThumbPrint -IncludeEqual -ExcludeDifferent
    #$CertSame=Compare-Object -ReferenceObject $CertAll -Property ThumbPrint -IncludeEqual -ExcludeDifferent
    $CertSame | foreach{GCI -filter "" –path Cert:\LocalMachine\My\$($_.thumbprint)} | Select-Object -Property Issuer, @{n=’ExpireInDays’;e={($_.notafter – (Get-Date)).Days}} -First 1
    

    感谢@bruce-zhang

    【讨论】:

      猜你喜欢
      • 2018-10-04
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多