使用Powershell比较证书

时间:2013-11-25 16:56:38

标签: powershell x509certificate

我正在开展灾难恢复项目,我建议作为计划的一部分,定期审核主要和次要网站。其中一项审计任务是确保辅助站点安装的证书与主站点相同。我想我可以使用Powershell

来实现这一目标
Get-ChildItem -Path Cert:\LocalMachine\My
Get-ChildItem -Path Cert:\LocalMachine\Root

我知道我可以使用上面的命令获取证书列表,但我遇到的问题是尝试在一个脚本中完成所有操作。我想在一台服务器上获取证书列表,然后在另一台服务器上获取证书列表,然后比较两个列表。我对Powershell很新,所以我不确定从哪里开始。

1 个答案:

答案 0 :(得分:2)

要检索证书,您将使用基础.NET类,因为证书提供程序默认情况下不会公开远程计算机连接。您可能还会发现PS远程处理的另一种可能性。这是功能:

function Get-Certificates {
    Param(
            $Computer = $env:COMPUTERNAME,
            [System.Security.Cryptography.X509Certificates.StoreLocation]$StoreLocation,
            [System.Security.Cryptography.X509Certificates.StoreName]$StoreName
          ) 

    $Store = New-Object System.Security.Cryptography.X509Certificates.X509Store("\\$computer\$StoreName",$StoreLocation)
    $Store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]"ReadOnly")
    $Store.Certificates
}

以下是如何使用它来比较两个列表:

$Left = Get-Certificates -StoreLocation LocalMachine -StoreName Root
$Right = Get-Certificates -StoreLocation LocalMachine -StoreName Root -Computer "REMOTE-PC"

# Dump to console
Compare-Object $Left $Right -property Thumbprint, FriendlyName, Subject, NotAfter | Format-Table

# Export results to file
Compare-Object $Left $Right -property Thumbprint, FriendlyName, Subject, NotAfter | Export-Csv Comparison.csv
相关问题