Friday, 15 February 2013

Powershell -match and weird results -



Powershell -match and weird results -

i having issue -match feature in conditional statement in powershell.

say have 3 files:

test.dll (this dll has [testclass] word in it)

test1. dll (this dll has testclass without brackets)

test2. dll (this dll doesn't have @ all)

i searching files have [testclass]

if utilize next code, both test.dll , test1.dll work in condition: (by way, each $_ dll)

if ([io.file]::readalltext($_) -match "testclass") { $projecttestcontainers += $_ write-host $projecttestcontainers }

which should, that's not want. want files contain [testclass] tried this

if ([io.file]::readalltext($_) -match "`[testclass`]") { $projecttestcontainers += $_ write-host $projecttestcontainers }

this works conditions, test.dll, test1.dll , test2.dll, shouldn't. should give me test.dll because 1 contains [testclass]

any ideas? allow me know if need more me.

the -match operator doing regular look comparing against 'testclass' string, backtick (`) character not right escape character regular look patterns. right in thinking bracket characters need escaped, since special characters in regular look pattern. because we're dealing regex, right escape character '\'; seek instead:

if ([io.file]::readalltext($_) -match "\[testclass\]")

powershell match

No comments:

Post a Comment