首页 > 解决方案 > Why doesn't Select-String filter the input of a ToString() method?

问题描述

Why doesn't powershell filter the output of an object's toString method?

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

Instead I just get everything I would normally get by running

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

I was expecting that like grep or findstring I would get just the lines that match the regex.

One would think that once $_.ToString() is called, you would just get string output...instead, am I just getting objects, or an array of strings?

标签: powershellwindows-10select-string

解决方案


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

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


推荐阅读