Powershell输出-重新排列结果以每行显示一个证书



我正在使用一个脚本从远程服务器获取证书,这是一个了不起的工作。但是我没有成功地使它在一行上显示一个证书。

$Servers = "srv01-corp-srv-name"
$Results = @()
$Results = Invoke-Command -cn $Servers {
$Certs = @{} | Select Certificate,Expired
$Cert = Get-ChildItem Cert:LocalMachineMy <#| Where-Object {$_.subject -match [Environment]::GetEnvironmentVariable("computername")} #>
If($Cert){
$Certs.Certificate = $Cert.subject
$Certs.Expired = $Cert.NotAfter
}
Else{
$Certs.Certificate = " - "
$Certs.Expired = " - "
}
$Certs
} | Select-Object @{n='ServerName';e={$_.pscomputername}},Certificate,Expired

#Display results in console
$Results | Sort-Object Expired -Descending

输出如下所示:

ServerName           Certificate                                                Expired
srv01-corp-srv-name  {CN=app.corp.com, CN=otherapp.corp.com, CN=last.corp.com}  2/21/2024 10:12:06 AM 2/26/2021 1:16:33 AM 11/6/2030 8:20:24 AM

但是我希望得到这样的结果:

ServerName           Certificate             Expired
srv01-corp-srv-name  {CN=app.corp.com}       2/21/2024 10:12:06 AM
srv01-corp-srv-name  {CN=otherapp.corp.com}  2/26/2021 1:16:33 AM
srv01-corp-srv-name  {CN=last.corp.com}      11/6/2030 8:20:24 AM

显示屏可能是那样显示的吗?提前感谢!

不要将Get-ChildItem cert:...的所有输出混合到单个对象中。相反,使用Select-Object来重命名所需的属性:

$Results = Invoke-Command -cn $Servers {
$Certs = Get-ChildItem Cert:LocalMachineMy <#| Where-Object {$_.subject -match [Environment]::GetEnvironmentVariable("computername")} #>
if($Certs){
$Certs
}
else {
# output a single empty dummy object
[pscustomobject]@{ Subject = ' - '; NotAfter = ' - '}
}
} | Select-Object @{n='ServerName';e={$_.pscomputername}},@{Name='Certificate';Expression='Subject'},@{Name='Expired'; Expression='NotAfter'}

相关内容

  • 没有找到相关文章

最新更新