简体   繁体   中英

Powershell Certificate Extensions export-csv

I'm trying to gather certificate extensions into a csv. The PowerShell commands seem to display just fine within PowerShell but when piped to a csv they display other types of data?

$cert = Get-ChildItem cert:\localmachine -Recurse
($cert.Extensions | Where-Object {$_.Oid.FriendlyName -eq "Key Usage"}).Format(1) | Export-Csv C:\Folder\File.csv

After searching the web I tried something different but still was unable to get the data to get captured and displayed as it does within PowerShell.

$cert=Get-ChildItem cert:\localmachine -Recurse           
$sanExt=$cert.Extensions | Where-Object {$_.Oid.FriendlyName -match "subject alternative name"}            
$sanObjs = new-object -ComObject X509Enrollment.CX509ExtensionAlternativeNames            
$altNamesStr=[System.Convert]::ToBase64String($sanExt.RawData)            
$sanObjs.InitializeDecode(1, $altNamesStr)            
Foreach ($SAN in $sanObjs.AlternativeNames) {$SAN.strValue}
$sanExt.Format(1) | Export-Csv C:\Folder\File.csv

For others looking to also gather the extension fields, this was my resolution:

$cert = Get-ChildItem cert:\localmachine -Recurse
($cert.Extensions | Where-Object {$_.Oid.FriendlyName -eq "Key Usage"}).Format(1) |
Select-Object -Property @{Name="Certificate";Expression={$_}} |
Export-csv -Path C:\Folder\file.csv`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM