我正在使用腳本遠程從服務器獲取證書,這非常棒。但我沒有成功讓它在一排顯示一張證書。
$Servers = "srv01-corp-srv-name"
$Results = @()
$Results = Invoke-Command -cn $Servers {
$Certs = @{} | Select Certificate,Expired
$Cert = Get-ChildItem Cert:\LocalMachine\My <#| 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
有沒有可能是這樣顯示的?先感謝您!
uj5u.com熱心網友回復:
不要將所有輸出混合Get-ChildItem cert:\...到一個物件中。相反,用于Select-Object重命名所需的屬性:
$Results = Invoke-Command -cn $Servers {
$Certs = Get-ChildItem Cert:\LocalMachine\My <#| 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'}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/474173.html
標籤:电源外壳
上一篇:以其他用戶身份匯入PFX-模擬
