【问题标题】:Why doesn't Select-String filter the input of a ToString() method?为什么 Select-String 不过滤 ToString() 方法的输入?
【发布时间】:2019-04-10 23:07:24
【问题描述】:

为什么 powershell 不过滤对象的 toString 方法的输出?

Get-ChildItem cert:\localmachine\my | % { Select-String -InputObject $_.ToString() -Pattern 'testcert' -SimpleMatch }

相反,我只是获得了通常通过跑步获得的一切

Get-ChildItem cert:\localmachine\my | % { $_.ToString() }

我期待像grepfindstring 这样我会得到与正则表达式匹配的行。

有人会认为,一旦调用$_.ToString(),你只会得到字符串输出……相反,我只是得到对象还是字符串数组?

【问题讨论】:

  • 我怀疑 - 在这一点上 - 你不是在看字符串的集合。 [grin] 这意味着您一次只针对一个字符串运行。

标签: powershell windows-10 select-string


【解决方案1】:

Select-String 适用于单个字符串 ...而您只是给它 - 单个多行字符串。 [咧嘴一笑]

如果要匹配证书属性中的某个字符串,请使用Where-Object {$_.PropName -match 'TestValue'} 获取命名属性中包含测试值的对象。

【讨论】:

  • 哦,我忘了 Where-Object...当输出看起来像一堆文本而不是成员时,我总是想 grep。
  • @leeand00 - 是的,习惯于文本解析的人们在看到 显示系统 显示为字符串时确实会得到信息一个洞......但实际上是一个字符串 -化复杂对象。我发现经常使用.GetType() 很方便... [grin]
【解决方案2】:

Lee_Daily 已经为你提供了答案,但你也做了以下...

(Get-ChildItem cert:\localmachine\my) -match 'testcert'

在我的一台测试机器上的示例

Get-ChildItem cert:\localmachine\my


PSParentPath: Microsoft.PowerShell.Security\Certificate::localmachine\my

Thumbprint    Subject 
----------    ------- 
FEB8E79E06... CN=NVIDIA GameStream Server         
D2D983C386... CN=Windows Admin Center             
96A0413F93... CN=Windows Admin Center             
5299896B41... CN=localhost                        



(Get-ChildItem cert:\localmachine\my) -match 'admin'


PSParentPath: Microsoft.PowerShell.Security\Certificate::localmachine\my

Thumbprint    Subject 
----------    ------- 
D2D983C386... CN=Windows Admin Center             
96A0413F93... CN=Windows Admin Center             



(Get-ChildItem cert:\localmachine\my) -match 'localhost'


PSParentPath: Microsoft.PowerShell.Security\Certificate::localmachine\my

Thumbprint    Subject 
----------    ------- 
5299896B41... CN=localhost

【讨论】:

    猜你喜欢
    • 2018-07-28
    • 2010-10-07
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    相关资源
    最近更新 更多