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

标签 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}

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

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

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

最佳答案

你可以这样做:

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

关于PowerShell:从格式化为列表的输出中解析特定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35760170/

相关文章:

pdf - 在哪里可以获得文档签名证书

powershell - 无提示提升 - 动词 runas start-process

macos - 在 AppleScript 或 PowerShell 中通过网络重新启动多台 Mac 的脚本?

email - 在 Firefox Phone 邮件应用程序中接受邮件证书

ios - 作为团队成员在两台不同的 Mac 上进行开发

tomcat - 将自签名证书链添加到 keystore

Python - SSL 客户端/服务器证书

arrays - 如何从 json 文件中删除空对象

powershell - 使用 Powershell 运行 Cake 脚本时,Jenkins 中出现未知命令错误

windows - windows环境变量名可以带点吗?