Im running the code below and it works:
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
I'm trying to write each line with expiring certificates with red color. If I try this:
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
All lines become red. If I try to pass $_ value to a If, the console says that is a Null value.
Any suggestions?
The solution for what I wanted to do was provided by mklement0. I will leave what I did here, if someone has the same question:
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 $_
}