【问题标题】:PowerShell: Parse Specific Item from Output Formatted as ListPowerShell:从格式化为列表的输出中解析特定项目
【发布时间】:2016-03-03 09:30:11
【问题描述】:

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

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

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 根证书颁发机构"

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

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}

但是,我想获得“主题”行,以便我的输出是单行。理想情况下,输出看起来像

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

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

【问题讨论】:

    标签: powershell certificate


    【解决方案1】:

    你可以这样做:

    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
    

    【讨论】:

      猜你喜欢
      • 2023-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      相关资源
      最近更新 更多