簡體   English   中英

如何使用 Powershell 腳本導出與特定 IIS 站點相關的證書詳細信息

[英]How to export the Certificate details related to the particular IIS site using Powershell script

我編寫了一個腳本,其中將所有 SSL 證書詳細信息從我的機器導出到 Excel 工作表,但我需要導出映射到 IIS 中特定站點的證書,然后我需要使用站點名稱和證書詳細信息到 Excel 工作表。

代碼

#Clearing the Console host in PS
Clear-Host

#Installing the Excel module to the Powershell
Install-Module -Name ImportExcel

#List of Servers
$computers = Get-Content "C:\TEMP\servers.txt" 

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

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

Invoke-Command -ComputerName $computers { 
    Get-ChildItem -Path 'Cert:\LocalMachine\My' -Recurse |
    Select-Object -Property @{n='ServerName';e={$env:COMPUTERNAME}},Issuer, Subject, NotAfter, 
    #@{Label = 'ServerName';Expression = {$env:COMPUTERNAME}}
    @{Label='Expires In (Days)';Expression = {(New-TimeSpan -Start (Get-Date) -End $PSitem.NotAfter).Days}} 
} | Export-Excel -Path C:\users\$env:username\documents\MultipleServer_Certificate_Expiry_Details.xlsx`

這是很常見的事情,網上有很多關於這個 IIS 用例的文章和示例。 這就是 Web 管理模塊的用途。

<# 
Get all IIS bindings and SSL certificates
On a local or remote IIS PowerShell Session
#>

Import-Module -Name 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
            CertificateFriendlyName      = $certificate.FriendlyName
            CertificateDnsNameList       = $certificate.DnsNameList
            CertificateNotAfter          = $certificate.NotAfter
            CertificateIssuer            = $certificate.Issuer
        }
    }
}

自定義上述內容以滿足您的輸出需求。

請注意,如果您碰巧使用的是舊版 PowerShell:

[PsCustomObject]@{} 在 PS 2.0 中不起作用,但您可以將其替換為 New-Object -TypeName PSObject

更新

您已要求在多台服務器上運行示例腳本。 但是,您的帖子中已經有了代碼。 只需將該 Invoke-Command 放在 ForEach 循環中並傳入計算機列表。

$Computers |  
ForEach {
    Invoke-Command -ComputerName $PSItem -ScriptBlock { 
        Get-ChildItem -Path 'Cert:\LocalMachine\My' -Recurse |
        Select-Object -Property @{n='ServerName';e={$env:COMPUTERNAME}},Issuer, Subject, NotAfter, 
        @{Label='Expires In (Days)';Expression = {(New-TimeSpan -Start (Get-Date) -End $PSitem.NotAfter).Days}} 
    } | Export-Excel -Path "C:\users\$env:username\documents\MultipleServer_Certificate_Expiry_Details.xlsx"
}

當然,您需要將 Web Admin 塊的示例添加到您的證書數據點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM