获取证书的指纹

时间:2014-03-14 14:41:57

标签: powershell certificate

我想将证书的指纹存储在如下变量中:

$thumbprint = 0F273F77B77E8F60A8B5B7AACD032FFECEF4776D

但我的命令输出是:

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

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

我需要删除除输出的指纹之外的所有内容

有什么想法吗?

4 个答案:

答案 0 :(得分:28)

您只需将命令括在括号中,然后使用点符号访问Thumbprint属性。

试试这个:

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

如果从命令中获得多个证书,则必须使用-join PowerShell运算符将指纹连接成单个字符串。

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

答案 1 :(得分:1)

您可以使用Select-Object仅获取Thumbprint - 属性:

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

答案 2 :(得分:0)

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

由于您正在使用$_.subject,而您应该使用$_.thumbprint,因此此操作将永远无法工作:

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

答案 3 :(得分:0)

在运行此命令后填充变量$ Thumbprint -> $Thumbprint = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -match "XXXXXXX"}

您可以在变量$ Thumbprint之后使用点符号来访问指纹,如下所示: -> $Thumbprint.Thumbprint

这样,您还可以访问证书的主题 -> $Thumbprint.Subject

在这种情况下,重命名变量会更有意义。 :)