Home > front end >  Powershell output - re-araging result to display one certificate on row
Powershell output - re-araging result to display one certificate on row

Time:05-13

I am using a script to get the certificates from servers remotely which does an amazing job. But I do not succeed in making it display one certificate on one row.

$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

The output looks like this:

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

But I would like to get this kind of result:

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

Is it possible that the display is shown like that? Thank you in advance!

CodePudding user response:

Don't mash all the output from Get-ChildItem cert:\... into a single object. Instead, use Select-Object to rename the desired properties:

$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'}
  • Related