powershell 脚本检查已安装证书

版权声明:authored by zzubqh https://blog.csdn.net/qq_36810544/article/details/82379727

额,前面的脚本能自动安装了,客户来了句我怎么知道已经安装好了?
我答:不是已经显示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的语法而已

猜你喜欢

转载自blog.csdn.net/qq_36810544/article/details/82379727