为什么 Select-String 不过滤 ToString() 方法的输入?

Why doesn't Select-String filter the input of a ToString() method?

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

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

相反,我只是得到了我通常会得到的所有东西 运行

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

我原以为 grepfindstring 我会得到与正则表达式匹配的行。

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

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

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

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