Hi, I have some clunky code and I'm sure it can be better. Looking for suggestions. I search a file and to file lines before a search pattern. Sample log file.
Date Line 1 - Blah Blah Blah Dynamic Text
Date Line 2 - foo bar 2
Date Line 3 - foo bar 3
Date Line 4 - foo bar 4
In the example below, $searchPattern = "foo bar 4". What I want to find is all occurrences of "Blah Blah Blah" and capture the Dynamic Text and only get a result set that is distinct (I don't want the same Dynamic Text duplicated). the Code below sort of works, I'm stuck trying to substring out just want I need. Thought I could do something like this: select PATINDEX('Blah\sBlah\sBlah.*', $newStringArray), but the interpreter does not like it, even tried moving it to a string.
Any suggestions?
Thank you!
foreach ($element in $drives) {
write-host "Searching $element\$FileName"
$foundArray = get-childitem $element\$FileName | select-string -Context 6,0 -pattern $searchPattern
if ($foundArray[0] -ne " ") {
$newArray =@()
$newstringArray =@()
write-host $foundArray.Count
#Split up the log lines via carriege returns
$newArray = $foundArray -split '[\r\n]' |? {$_}
$newStringArray = $newArray | select-string "Blah Blah Blah"
write-host $newstringArray[0]
break
}
}Chris