gpt4 book ai didi

powershell - 获取证书的预期目的

转载 作者:太空宇宙 更新时间:2023-11-03 14:26:54 25 4
gpt4 key购买 nike

我有这段代码可以获取有关“本地机器”SSL 证书的信息并将它们存储在 CSV 文件中。我担心的是我没有找到定义这些证书的正确名称。所以我想问有没有表示证书名称的字段?

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

最后一个问题,我发现有些证书的“Intended Purposes”字段有一个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'

最佳答案

以下脚本迭代 Cert Extensions,如果是 Usage,则存储在变量 $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

关于powershell - 获取证书的预期目的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56254011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com