Вопрос

I want to store the thumbprint of a certificate in a variable like this:

$thumbprint = 0F273F77B77E8F60A8B5B7AACD032FFECEF4776D

But my command output is:

Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -match "XXXXX"}

Thumbprint                                Subject 
----------                                ------- 
0F273F77B77E8F60A8B5B7AACD032FFECEF4776D  CN=XXXXXXX, OU=YYYYYYY 

I need to remove everything but the thumbprint of that output

Any idea?

Это было полезно?

Решение

All you have to do is wrap the command in parentheses, and then use dot-notation to access the Thumbprint property.

Try this out:

$Thumbprint = (Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -match "XXXXXXX"}).Thumbprint;
Write-Host -Object "My thumbprint is: $Thumbprint";

If you get multiple certificates back from your command, then you'll have to concatenate the thumbprints into a single string, perhaps by using the -join PowerShell operator.

$Thumbprint = (Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -match "XXXXXXX"}).Thumbprint -join ';';
Write-Host -Object "My thumbprints are: $Thumbprint";

Другие советы

You can use Select-Object to get only the Thumbprint-property:

Get-ChildItem -Path Cert:\LocalMachine\My | 
    Where-Object {$_.Subject -match "XXXXXXX"} | 
    Select-Object -ExpandProperty Thumbprint
Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -match "XXXXXXX"} 

This one will never work cause you are using $_.subject you shall use $_.thumbprint:

Get-ChildItem -Path Cert:\LocalMachine\My |  Where-Object {$_.Thumbprint -match "0F273F77B77E8F60A8B5B7AACD032FFECEF4776D"}

Once your variable $Thumbprint is populated after running this command --> $Thumbprint = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -match "XXXXXXX"}

You can access the thumbprint by using the dot-notation after your variable $Thumbprint like this --> $Thumbprint.Thumbprint

This way you can also access the Subject of the certificate --> $Thumbprint.Subject

Renaming the variable would make more sense in this case. :)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top