获取证书目的

时间:2019-05-22 09:49:49

标签: powershell ssl

我有这段代码可获取有关“本地计算机” SSL证书的信息,并将其存储在CSV文件中。我担心的是,我没有找到定义这些证书的专有名称。所以我问是否有一个字段指示证书名称?

此外,我想存储SSL证书的“预期目的”,但我不知道该怎么做。

最后一个问题,我发现有些证书的“预期目的”字段具有ALL值,所以我想知道在这种情况下提到的目的是什么?

这是我的脚本,我希望它也向我显示“目的”以及如果找到证书的名称。

$StartDate = Get-Date
$CertPath = 'Cert:\LocalMachine\'
$CertsDetail = Get-ChildItem -Path $CertPath -Recurse | Where-Object {
    $_.PsIsContainer -ne $true
} | ForEach-Object {
    $DaysLeft = (New-TimeSpan -Start $StartDate -End $_.NotAfter).Days
    if ($DaysLeft -lt 1) {
        $Under30 = $true
        $Expired = $true
        $Text = "The Certificate is expired"
    } elseif ($DaysLeft -lt 30) {
        $Under30 = $true
        $Expired = $false
        $Text = "The Certificate is but valid about to expire"
    } else {
        $Under30 = $false
        $Expired = $false
        $Text = "The Certificate is still valid and not going soon to expire"
    }
    $FinalDate = Get-Date $_.NotAfter -Format 'dd/MM/yyyy hh:mm'

    [PSCustomObject]@{
        Text = $Text
        Subject = $_.Subject
        ExpireDate = $FinalDate
        DaysRemaining = $DaysLeft
        Under30Days = $Under30
        Expired = $Expired
    }
}
$CertsDetail | Where-Object {
    $_.DaysRemaining -lt 3650
} | Export-Csv -NoTypeInformation -Path 'C:\SECnology\Data\Utilities\Certificate_State.csv'

1 个答案:

答案 0 :(得分:0)

以下脚本会迭代证书扩展,如果使用的话,将存储在变量$ Usage中,该变量已合并到[PSCustomObject]

编辑:结合了JosefZ的宝贵提示

## Q:\Test\2019\05\22\SO_56254011.ps1
$StartDate = Get-Date
$CertPath  = 'Cert:\LocalMachine\'
$FileOut   = 'C:\SECnology\Data\Utilities\Certificate_State.csv'
$CertsDetail = Get-ChildItem -Path $CertPath -Recurse | Where-Object {
    $_.PsIsContainer -ne $true
} | ForEach-Object {
    $DaysLeft = (New-TimeSpan -Start $StartDate -End $_.NotAfter).Days
    if ($DaysLeft -lt 1) {
        $Under30 = $true
        $Expired = $true
        $Text = "The Certificate is expired"
    } elseif ($DaysLeft -lt 30) {
        $Under30 = $true
        $Expired = $false
        $Text = "The Certificate is but valid about to expire"
    } else {
        $Under30 = $false
        $Expired = $false
        $Text = "The Certificate is still valid and not going soon to expire"
    }
    $FinalDate = Get-Date $_.NotAfter -Format 'dd/MM/yyyy hh:mm'
    $Usages = foreach($key in $_.Extensions){
      if('KeyUsages' -in $key.psobject.Properties.Name ){ $key.KeyUsages}
      if('EnhancedKeyUsages' -in $key.psobject.Properties.Name){
          $key.EnhancedKeyUsages.FriendlyName
      }
    }
    [PSCustomObject]@{
        Text         = $Text
        Subject       = $_.Subject
        ExpireDate    = $FinalDate
        DaysRemaining = $DaysLeft
        Under30Days   = $Under30
        Expired       = $Expired
        Usages        = $Usages-join ';'
    }
}
$CertsDetail | Out-Gridview
$CertsDetail | Where-Object {
    $_.DaysRemaining -lt 3650
} | Export-Csv -NoTypeInformation -Path $FileOut

样本输出。

Text          : The Certificate is expired
Subject       : CN=SITHS CA v3, O=Carelink, C=SE
ExpireDate    : 28-11-2015 07:02
DaysRemaining : -1271
Under30Days   : True
Expired       : True
Usages        : CrlSign, KeyCertSign
相关问题