i have the following script to get lines of interest. the issue i have is just the one matching PRT0009 i need one additional following line for the rest of the error message im not having luck trying to use movenext. note this needs to be done in order just as the foreach loop would produce as is without the second line.
i want to output all lines from a log file containing one of the -match “errorIDs” in order. an example line looks like this…
2018-06-19 06:58:46[ERROR[SYS.Base.Run.Engine[PRT0009 - There is a communications problem between the PC and the printer. The following error has been generated:
how would this handle only one pattern having a -context 0,1? i have modified things to use something similar but still cannot find a way to get that extra line for only specific patterns while maintaining order of events.
Hrmm… nah, there’s a simpler way if you just want all lines containing any of those error IDs to be pulled out. No looping, no select-string. Just -match:
I did group the match string a bit to minimise unnecessary match steps for the regex parser, but you could just as easily have your match string be all your error IDs delimited by pipe symbols (pipe is the regex symbol for ‘or’).
This will return all lines from the log file that contain one of the IDs and store them into the file specified.
-match, much like every other operator that doesn’t normally get used with arrays, will pull out every array entry that matches the requisite pattern if applied to an array.
You can pipe out of ForEach-Object, but not the ForEach{} scripting construct. Anything in a ForEach-Object block that is written the pipeline will be emitted to the next command in the pipeline.
But the “right” pattern would be to build your own function to perform the filtering, right? The ForEach becomes implicit when you pipe input and process it in a PROCESS{} block.
That way your pattern-matching business becomes a self-contained tool, which can be used in a composed command line with other tools like Out-File. And then your inputs don’t necessarily have to be quoted, right? Put one per line in a text file and:
Most likely i will put this into a function as this part is just data gathering for a WPF GUI. The txt file output is just for me to evaluate results at this point. Going forward i intend to write a form of analytics to narrow down based on errors present and such what things the end user needs to be looking for as most likely cause of the problem on the machine. as things are now a flow chart to diagnose without understanding of the codes is a needle in a haystack, all this will still only get a general that direction pointer but how much better can emulated serial port communication get.