Select-String by pattern then if found actions


I don’t have your log to test but, some of your logic is a little hard to follow. Here’s what I think should work.


$SawLog = Get-Content "C:\temp\Complete.log"

$FullSelect += $SawLog -match "Part( Started -| Complete)|PRT(0004|0005|0009)|ENG(0029|0037)|OPR0078|WaitingForPrinterTrigger"

$GetData = $False

$Results = foreach ($line in ($FullSelect -split [environment]::NewLine)) {
    if ($line -match 'Part Started -') {
        #Start flag to gather data, reset array and reset $trap flag
        $GetData = $True
        $ProcessArray = @()
        $Trap = $False

    if ($GetData) {
        #Return the line and check for error to set trap
        if ($line -match "PRT(0004|0005|0009)|ENG(0029|0037)|OPR0078|WaitingForPrinterTrigger") {
            $trap = $True 
        $ProcessArray += $line 
    if ($line -match "Part Complete") {
        #reset the get data flag
        $GetData = $False
        if ($Trap) { $ProcessArray }


That works. Seems I like to over itemize tasks in the thought process of what the pipe is doing (pluralsight vids preach one task at a time). Building scripts to perform command like functions seems easier but parsing strings is looping and pipe intensive. Definitely learned on this script I didnt have any idea I could flag a line in a loop like that and started off with thinking I needed to define a start and end post error string match. Any who thank you all for the assistance hope this base gets me what I need to parse away for my ever growing WPF script.

One last tid bit hopefully an easy one. The Foreach inverted the entry order as in the list is now newest on top instead of bottom. Is there an easy way to invert that?