Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Select-object from cert store

Essentially I am trying to get the serial number of a certificate by using the subject so I can use it in a variable in CertUtil.

I am so close yet so far as you will see below can anybody help please?

cd cert:\localmachine\my

$serno = get-childitem | where { $_.subject -eq "XXXXXXXXXXXXXXXX" } | format-list     -property * | select -property SerialNumber

$serno

this displays

SerialNumber        
------------ 

not the actual serial number

Does anybody have any clues??

like image 987
choco_linux Avatar asked Feb 04 '14 11:02

choco_linux


1 Answers

Don't use format-list, you already have all the properties. format-list will convert your nice X509Certificate2 object into a set of format objects which isn't what you want at all. Also use -expandproperty on the select:

PS>get-childitem | where { $_.subject -eq "CN=localhost" } | select -expandproperty SerialNumber
6191F4A438FF77A24763E6D427749CD7

Or with Powershell 3 or later, use the shorthand notation:

PS>$serno = (get-childitem | where { $_.subject -eq "CN=localhost" }).SerialNumber
PS>$serno
6191F4A438FF77A24763E6D427749CD7
like image 149
Duncan Avatar answered Sep 28 '22 00:09

Duncan