2013-11-25 46 views
-1

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

Get-ChildItem -Path Cert:\LocalMachine\My 
Get-ChildItem -Path Cert:\LocalMachine\Root 

我知道我可以用上面的命令来获得证书的列表,但我有与正试图做到这一切在一个脚本什么麻烦。我想获得一台服务器上的证书列表,然后获取另一台服务器上的证书列表,然后比较两个列表。我对Powershell非常陌生,所以我不确定从哪里开始。

+3

你尝试过什么,并没有遇到什么问题,你需要哪些帮助? –

+0

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

+1

开始[这里](http://blogs.msdn.com/b/timid/archive/2009/10/07/powershell-for-non-n00bs-certificates-installed-on-a-remote-host.aspx) 。 –

回答

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