0

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?

leeand00
  • 25,510
  • 39
  • 140
  • 297
  • i suspect that - at that point - you are NOT looking at a collection of strings. [*grin*] that would mean that you are running against one string at a time. – Lee_Dailey Apr 10 '19 at 23:22

2 Answers2

1

Select-String works on individual strings ... and you are giving it just that - individual multiline strings. [grin]

if you want to match a certain string in the properties of the cert, use Where-Object {$_.PropName -match 'TestValue'} to get the object that contains the test value in the named prop.

Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26
  • Oh I forgot about Where-Object...when the output looks like a bunch of text instead of Members, I always want to grep. – leeand00 Apr 11 '19 at 01:08
  • 1
    @leeand00 - yep, folks who are accustomed to text parsing really get info a hole when seeing what the _display system_ shows as strings ... but is really a string-ified complex object. i find that frequent use of `.GetType()` is right handy ... [*grin*] – Lee_Dailey Apr 11 '19 at 01:20
0

Lee_Daily already provided you an answer, but you have also done the following...

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

Example on one of my test machines

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
postanote
  • 15,138
  • 2
  • 14
  • 25