此脚本收集有关本地计算机证书的信息,并显示证书过期的日期(以JSON格式)。
{ "{#CERTINFO}" : "@{Expires in (Days)=8074}" }
我需要的只是8074
(天),并且无法弄清楚如何编辑这个脚本。
$cert_ = get-childitem cert:LocalMAchine -recurse |
where-object {$_.NotAfter -gt (get-date)} |
select @{Name="Expires in (Days)";Expression={($_.NotAfter).subtract([DateTime]::Now).days}} |
Sort "Expires in (Days)"
write-host "{"
write-host " `"data`":[`n"
$idx = 1
foreach ($cert_arr in $cert_ )
{
if ($idx -lt $cert_.Count)
{
$line= "{ `"{#CERTINFO}`" : `"" + $cert_arr + "`" },"
write-host $line
}
elseif ($idx -ge $cert_.Count)
{
$line= "{ `"{#CERTINFO}`" : `"" + $cert_arr + "`" }"
write-host $line
}
$idx++;
}
write-host
write-host " ]"
write-host "}"
答案 0 :(得分:0)
我会尝试:
function Get-CertInfo
{
$cert_ = get-childitem cert:LocalMAchine -recurse | where-object {$_.NotAfter -gt (get-date)} | select @{Name="Expires in (Days)";Expression={($_.NotAfter).subtract([DateTime]::Now).days}} | Sort "Expires in (Days)"
$data = @()
foreach($c in $cert_)
{
$entry = [PSObject]@{"#CERTINFO"="$c"}
$data += $entry
}
$certInfor = [PSObject]@{"data"=$data}
$certInfor | ConvertTo-Json
}

这会产生:
{
"data": [
{
"#CERTINFO": "@{Expires in (Days)=221}"
},
{
"#CERTINFO": "@{Expires in (Days)=272}"
}
]
}
希望有所帮助。