输出某些格式的表格颜色

时间:2018-07-27 19:07:37

标签: powershell

我正在运行下面的代码,它可以正常工作:

Get-ChildItem -Path T: -Filter *.pfx | ForEach-Object { 
    Import-PfxCertificate -FilePath $_.FullName -CertStoreLocation Cert:\CurrentUser\My -Password ****** -Force -AsPlainText) -Exportable 
} |
Format-Table -Property @{Label="Computador"; Expression={$env:computername}},
    @{Label="Nome"; Expression={$_.FriendlyName}},
    @{Label="Validade"; Expression={$_.NotAfter}},
    @{Label="Impressão digital"; Expression={$_.Thumbprint}} -AutoSize

我正在尝试用过期的红色证书写每一行。 如果我尝试这样做:

Get-ChildItem -Path T: -Filter *.pfx | ForEach-Object { 
    Import-PfxCertificate -FilePath $_.FullName -CertStoreLocation Cert:\CurrentUser\My -Password ****** -Force -AsPlainText) -Exportable 
} |
Format-Table -Property @{Label="Computador"; Expression={$env:computername}},
    @{Label="Nome"; Expression={$_.FriendlyName}},
    @{Label="Validade"; Expression={$_.NotAfter}},
    @{Label="Impressão digital"; Expression={$_.Thumbprint}} -AutoSize |
Out-String | Write-Host -ForegroundColor Red

所有行变为红色。 如果我尝试将$ _值传递给If,则控制台会说这是一个Null值。

有什么建议吗?


mklement0提供了我想做的解决方案。如果有人有相同的问题,我将离开我在这里所做的一切:

Get-ChildItem -Path T: -Filter *.pfx | ForEach-Object {
Import-PfxCertificate -FilePath $_.FullName -CertStoreLocation Cert:\CurrentUser\My -Password ****** -Force -AsPlainText) -Exportable
            } | Format-Table -Property @{Label="Computador"; Expression={$env:computername}}, @{Label="Nome"; Expression={$_.FriendlyName}}, @{Label="Validade"; Expression={$_.NotAfter}}, @{Label="Status"; Expression={if (($_.NotAfter).subtract([DateTime]::Now).days -lt 0){"Expirado"} elseif (($_.NotAfter).subtract([DateTime]::Now).days -lt 30){"Expirando"}}},@{Label="Impressão digital"; Expression={$_.Thumbprint}} -AutoSize | Out-String -Stream |
            ForEach-Object {
                $fgArg = if ($_ -match 'Expirado') { @{ 'BackgroundColor' = 'Red'; 'ForegroundColor' = 'Black'} } elseif ($_ -match 'Expirando') { @{ 'BackgroundColor' = 'Yellow'; 'ForegroundColor' = 'Black'} } else { @{} }
                Write-Host @fgArg $_
            }

2 个答案:

答案 0 :(得分:4)

关键是使用 Out-String的{​​{1}}开关,该开关启用逐行处理;默认情况下,-Stream输出单个多行字符串。

这是一个简单的示例,将包含子字符串Out-String的那些行涂成红色(与PowerShell中一样,默认情况下,匹配不区分大小写):

expired

以上结果:

enter image description here


如果您正在寻找打包为高级功能的此功能,请参见function
'A valid certificate', 'An expired certificate', 'A valid certificate' | Out-String -Stream | ForEach-Object { $fgArg = if ($_ -match 'expired') { @{ 'ForegroundColor' = 'Red' } } else { @{} } Write-Host @fgArg $_ } 在我的this answer中。

答案 1 :(得分:0)

您正在尝试完成对于PowerShell而言相当困难的事情。我编写了一个脚本,可以帮助您实现目标https://gallery.technet.microsoft.com/scriptcenter/Highlight-String-and-45525e27

它并不十分优雅,但是您可以像这样使用它

gci cert: -Recurse | Format-Table -Property @{Label="Computador"; Expression={$env:computername}},@{Label="Exp"; Expression={if($_.NotAfter -le (get-date)){"Expired"}}}, @{Label="Nome"; Expression={$_.FriendlyName}}, @{Label="Validade"; Expression={$_.NotAfter}}, @{Label="Impressão digital"; Expression={$_.Thumbprint}},-AutoSize | ho '.*Expired.*'

祝你好运!

Shane