![](/img/trans.png)
[英]How to modify excel data and export to text file using PowerShell script?
[英]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.