Home > Back-end >  How do I add a noteproperty FULLNAME from a Get-ChildItem command listing of Certificate files and p
How do I add a noteproperty FULLNAME from a Get-ChildItem command listing of Certificate files and p

Time:01-13

How do I add a noteproperty FULLNAME from a Get-ChildItem command and pass that into the $obj New-Object "X509 Cryptography" which holds the certificate details? I want the path to the certificates to remain intact and attached to the next New-Object which has the actual certificate Noteproperties. Once I pass through my ForEach statement, I get the new-object NoteProperties containing certificate details (thumbrint, Issuer, etc) but lose the ability to retain FullName which is the path to the files.

$certlisting = Get-ChildItem -path $certpath1 -Recurse 

$certout = foreach ($cer in $certlisting.fullname){

$obj = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2($cer) | 
        select Issuer, FriendlyName, NotAfter, NotBefore, PublicKey, SerialNumber, SignatureAlgorithm, Subject, Thumbprint, Version |
        Sort-Object Issuer, NotAfter -Descending 
        $obj
    }

$certout | Export-Csv -Path .\Certificates.csv -NoTypeInformation -Encoding ASCII

If I add "FullName" to the line $obj = and say "select FullName, Issuer, FriendlyName etc" FullName doesn't contain any real data. I want the FULLNAME data from my original $certlisting.FullName to pass into the next $obj.

CodePudding user response:

To clarify my comments

Either change the code to this:

$certlisting = Get-ChildItem -path $certpath1 -Recurse 

$certout = foreach ($cer in $certlisting.FullName) {
    # here we capture the result in an intermediate variable $obj and utput that to be collected in $certout
    $obj = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2($cer) | 
           Select-Object Issuer, FriendlyName, NotAfter, NotBefore, PublicKey, SerialNumber, SignatureAlgorithm, 
                         Subject, Thumbprint, Version, @{Name = 'FullName'; Expression = {$cer}} |
           Sort-Object Issuer, NotAfter -Descending 
    $obj
}

$certout | Export-Csv -Path .\Certificates.csv -NoTypeInformation -Encoding ASCII

Or this:

$certlisting = Get-ChildItem -path $certpath1 -Recurse 

$certout = foreach ($cer in $certlisting.FullName) {
    # here we do not capture the result in an intermediate variable $obj, but output straight away
    New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2($cer) | 
    Select-Object Issuer, FriendlyName, NotAfter, NotBefore, PublicKey, SerialNumber, SignatureAlgorithm, 
                  Subject, Thumbprint, Version, @{Name = 'FullName'; Expression = {$cer}} |
    Sort-Object Issuer, NotAfter -Descending 
}

$certout | Export-Csv -Path .\Certificates.csv -NoTypeInformation -Encoding ASCII
  • Related