开发者

PowerShell - X509Certificates.X509Store get all certificates?

I want to get all certificates from my system. So I used the System.Security.Cryptography.X509Certificates class. When I re开发者_JAVA百科move the () after the X509Store I getting the same results like I entered "My"

What is the right membername to see all certificates? It is possible?

MSDN StoreName Enumeration

$store=new-object System.Security.Cryptography.X509Certificates.X509Store("CA")
# Put in CA, My, root etc.
$store.open("ReadOnly")
$store.Certificates
$store.Certificates.count 


You can get them from your local cert drive:

Get-ChildItem Cert:\CurrentUser\CA # user certs

Get-ChildItem Cert:\LocalMachine\CA # machine certs


Get-ChildItem Cert:\LocalMachine\My

This is fun if you have WinRM installed but in a much more standard way to find all certificate it is much better to use something like

$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("\\$server_name\My","LocalMachine")

$store.Open("ReadOnly")             
$store.Certificates


The following PowerShell script will ask for the DNS name of a remote computer, then it asks for Domain Admin credentials so it can connect and query. The resulting $AllCerts var has every certificate from every store. It then also exports them to a CSV file in the $env:temp folder and opens the folder in Windows Explorer.

function Get-Cert( $computer=$env:computername ){
    $cred = Get-Credential -Message "Enter credentials for a Domain Admin"
    $ro=[System.Security.Cryptography.X509Certificates.OpenFlags]"ReadOnly"
    $lm=[System.Security.Cryptography.X509Certificates.StoreLocation]"LocalMachine"    
    $Stores = (Invoke-Command $computer {Get-ChildItem cert:\LocalMachine} -Credential $cred).Name
    $AllStores = @()
    foreach ($store in $Stores){
        $AllStores += new-object System.Security.Cryptography.X509Certificates.X509Store("\\$computer\$store",$lm)
    }
    $AllStores.Open($ro)
    $AllStores.Certificates
}
write-host "Enter remote computer to poll certificate information from" -ForegroundColor Cyan
$remoteComputer = read-host
$AllCerts = Get-Cert $remoteComputer
$AllCerts = $AllCerts | Select Subject,Issuer,Thumbprint,NotBefore,NotAfter
$AllCerts | Where-Object {$_.NotAfter -lt (Get-Date)} | format-list 
$AllCerts | export-csv -NoTypeInformation $env:temp\$($remoteComputer)_AllCerts.csv
start $env:temp


Fantastic Script, I had issue with it naming and could be me easily, but changed this and very happy with the output, thanks! From:

$AllCerts | export-csv -NoTypeInformation $env:temp\$($remoteComputer)_AllCerts.csv
start $env:temp
To:
$AllCerts | export-csv c:\temp\AllCerts.csv -NoTypeInformation
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜