如何使用 Powershell 脚本导出与特定 IIS 站点相关的证书详细信息

如何使用 Powershell 脚本导出与特定 IIS 站点相关的证书详细信息

我编写了以下代码来仅导出与 IIS 站点相关的一台服务器的 SSL 证书详细信息,但我希望该脚本在多台服务器上运行。我该如何解决这个问题?

Import-Module -Name WebAdministration

#Number of days to look for expiring certificates
$threshold = 300

#Set deadline date
$deadline = (Get-Date).AddDays($threshold)

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]@{
      ServerName                = $env:COMPUTERNAME
      Sites                     = $_.Sites.Value
      CertificateIssuer         = $certificate.Issuer
      CertificateSubject        = $certificate.Subject
      CertificateDnsNameList    = $certificate.DnsNameList
      CertificateNotBefore      = $certificate.NotBefore
      CertificateNotAfter       = $certificate.NotAfter

      #CertificateExpiryInDays      = New-TimeSpan -Start (Get-Date) -End $PSitem.NotAfter.Days
    }
  }
}

相关内容