powershell 指令碼檢查已安裝證書
阿新 • • 發佈:2018-11-08
額,前面的指令碼能自動安裝了,客戶來了句我怎麼知道已經安裝好了?
我答:不是已經顯示success了嗎?
客戶:那以後呢,我忘了怎麼辦?
可以用mmc啊,然後選證書,本地計算機
mmc是什麼?
額……,我寫指令碼吧
==================讓心裡的神獸再奔騰一會兒的分割線================
function Check-Certificate
{
param
(
[string] $CertName
)
# 我安裝在了LocalMachine的My下,根據實際情況修改
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store "My", "LocalMachine"
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$storecollection = [System.Security.Cryptography.X509Certificates.X509Certificate2Collection]$store.Certificates
$find = 0
foreach ($x509 in $storecollection)
{
$sub = $x509.Subject
if($sub.contains($CertName))
{
$find = 1
break
}
}
if($find -eq 1)
{
"'$CertName' certificate has been Successfully installed!"
}
else
{
"could not fond '$CertName' please run setup.ps1 frist!"
}
$store.Close()
}
Check-Certificate -CertName "要查詢證書的subject"
Write-Host 'Press Any Key to exist!' -NoNewline
$null = [Console]::ReadKey('?')
其實就是呼叫System.Security.Cryptography.X509Certificates.*下的類,與用.net差不多,只是要遵循ps1的語法而已