首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Powershell脚本导出与特定IIS站点相关的证书详细信息

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

Stack Overflow用户
提问于 2020-02-05 11:43:23
回答 1查看 1.6K关注 0票数 1

我编写了一个脚本,其中将将所有SSL证书详细信息从我的计算机导出到Excel表,但我需要导出映射到IIS中特定站点的证书,然后需要将这些包含站点名称和证书详细信息的详细信息导出到Excel工作表中。

代码语言:javascript
运行
复制
#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`
EN

Stack Overflow用户

发布于 2020-02-06 00:24:49

这是一件非常常见的事情,在这个IIS用例上,web上有很多文章和示例。这就是web管理模块的用途。

代码语言:javascript
运行
复制
<# 
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@{}不会在PS2.0中工作,但您可以用新对象-TypeName PSObject替换它

更新

您已经要求在多个服务器上运行一个示例脚本。然而,您已经在您的帖子中有代码了。只需将调用命令放在ForEach循环中,并传入计算机列表即可。

代码语言:javascript
运行
复制
$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块的示例添加到您的cert数据点中

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60075064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档