PowerShell:从输出格式化为列表的特定项解析

时间:2016-03-02 23:18:01

标签: powershell certificate

我需要以编程方式确定是否涉及特定证书,这是微不足道的。但是,我希望 的输出包含所述证书的主题行。

使用以下内容,我可以获得已安装证书的列表:

  

Get-ChildItem -Recurse Cert:

========================================================================

Subject      : CN=VeriSign Class 3 Public Primary Certification Authority - G5
Issuer       : CN=VeriSign Class 3 Public Primary Certification Authority - G5
Thumbprint   : 4EB6D578499B1CCF5F581EAD56BE3D9B6744A5E5
FriendlyName : VeriSign
NotBefore    : 11/7/2006 6:00:00 PM
NotAfter     : 7/16/2036 6:59:59 PM
Extensions   : {System.Security.Cryptography.Oid, System.Security.Cryptography.Oid}

Subject      : CN=Go Daddy Root Certificate Authority - G2, O="GoDaddy.com, Inc.", L=Scottsdale, S=Arizona, C=US
Issuer       : CN=Go Daddy Root Certificate Authority - G2, O="GoDaddy.com, Inc.", L=Scottsdale, S=Arizona, C=US
Thumbprint   : 47BEABC922EAE80E78783462A79F45C254FDE68B
FriendlyName : Go Daddy Root Certificate Authority – G2
NotBefore    : 8/31/2009 7:00:00 PM
NotAfter     : 12/31/2037 5:59:59 PM
Extensions   : {System.Security.Cryptography.Oid, System.Security.Cryptography.Oid, System.Security.Cryptography.Oid}

Subject      : CN=StartCom Certification Authority, OU=Secure Digital     Certificate Signing, O=StartCom Ltd., C=IL
Issuer       : CN=StartCom Certification Authority, OU=Secure Digital    Certificate Signing, O=StartCom Ltd., C=IL
Thumbprint   : 3E2BF7F2031B96F38CE6C4D8A85D3E2D58476A0F
FriendlyName : StartCom Certification Authority
NotBefore    : 9/17/2006 2:46:36 PM
NotAfter     : 9/17/2036 2:46:36 PM
Extensions   : {System.Security.Cryptography.Oid, System.Security.Cryptography.Oid.}

使用这个我只能找到具有特定字符串的部分:

  

Get-ChildItem -Recurse Cert:| select-string" CN = Go Daddy Root Certificate Authority"

========================================================================

Subject      : CN=Go Daddy Root Certificate Authority - G2, O="GoDaddy.com, Inc.", L=Scottsdale, S=Arizona, C=US
Issuer       : CN=Go Daddy Root Certificate Authority - G2, O="GoDaddy.com, Inc.", L=Scottsdale, S=Arizona, C=US
Thumbprint   : 47BEABC922EAE80E78783462A79F45C254FDE68B
FriendlyName : Go Daddy Root Certificate Authority – G2
NotBefore    : 8/31/2009 7:00:00 PM
NotAfter     : 12/31/2037 5:59:59 PM
Extensions   : {System.Security.Cryptography.Oid, System.Security.Cryptography.Oid, System.Security.Cryptography.Oid}

但是,我想 <&#34;主题&#34;行,以便我的输出是一行。理想情况下,输出看起来像

CN=Go Daddy Root Certificate Authority - G2, O="GoDaddy.com, Inc.", L=Scottsdale, S=Arizona, C=US

但只要单行输出只包含有问题的证书,结果就可以了。

1 个答案:

答案 0 :(得分:4)

您可以执行以下操作:

Get-ChildItem -Recurse Cert: | Where-Object { $_.Subject -like 'CN=Go Daddy Root Certificate Authority*' } | Select Subject

使用Select-String会让你丢失你感兴趣的对象,然后你就会处理字符串。

请注意,您可以通过使用类似比较中的其他属性(例如FriendlyName)来查找您要查找的特定证书。例如:

Get-ChildItem -Recurse Cert: | Where-Object { $_.FriendlyName -like 'Go Daddy Root Certificate Authority*' } | Select Subject