2013-11-25 47 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