简体   繁体   中英

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

I wrote a script where in it will export all the SSL certificate details from my machine to an Excel sheet, but I need to export the Certificates which are mapped to the particular site in IIS and then I need to export those details with Site name and the Certificate details to an Excel sheet.

Code

#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`

This is a very common thing, with many articles and samples all over the web on this IIS use case. This is what the web administration module is used for.

<# 
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
        }
    }
}

Customize the above to fit your output needs.

Note if you happen to be on a legacy version of PowerShell:

[PsCustomObject]@{} will not work in PS 2.0 but you may replace it by New-Object -TypeName PSObject

Update

You've asked for a sample script to run on multiple servers. However, you already have the code in your post. Just put that Invoke-Command inside a ForEach loop and pass in a list of computers.

$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"
}

Of course, you'll need to add in that sample for the Web Admin block to your cert data points

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM