2

I'm working on a powershell script to find offline devices and email a list to users on a daily basis. I have most of the script complete except for one piece. I output the inner text from a website to a text file, then use powershell to scrape that text file and pull lines containing a number of certain strings. The contents of the text file will be in the following format:

Area: 15NT_ Rm: Rm_42 Adr: 3 Type: CL41 - Issues Supervision:

Adr: 2 Type: Bed1P - OFFLINE Supervision:

Adr: 3 Type: Bed2 - FB-module missing Supervision:

Adr: 4 Type: PC - OFFLINE Supervision:

For the text above, 2 lines contain the string "OFFLINE" which is one of the strings I would look for. For the text above, I would want the script to extract from the very last line with the string "OFFLINE" and all previous lines, up to and including the one that starts with the string "Area", then append those lines to a text file.

Edit: Here is the original code I was using:

$log = get-content C:\temp\RGS.txt
foreach ($line in $log) { 
if ($line -like "*Area*" -OR $line -like "*missing*" -or $line -like 
"*OFFLINE*" -or $line -like "*internal*" -or $line -like "*configuration*" - or $line -like "*unknown*") {
$line | out-file -FilePath "C:\temp\RGS Extract.txt" -Append
    }
}

Also, in the source text, there will be multiple lines that start with the word "Area", but that won't have an "Adr" listed underneath it before the next "Area". I wouldn't want those lines pulled since the area doesn't have addresses that contain one of the keywords I'm looking for. Basically whenever a line starts with "Adr", and contains one of the key words, I would want to add that line, and all previous lines up to and including the line with the first occurrence of the word "Area".

Here's a larger sample of the data source I'm working with:

Area: 7NT_ Rm: RM_11 Adr: 26 Type: CL41 - Issues Supervision: 
Adr: 2 Type: Bed1P - OFFLINE Supervision: 
Adr: 3 Type: Bed2 - FB-module missing Supervision: 
Adr: 6 Type: PCAudio Supervision: 
Adr: 7 Type: PC Supervision: 
Adr: 12 Type: PB2C Supervision: 
Adr: 15 Type: Jack2 Supervision: 
Area: 7NT_ Rm: Nourish_7116 Adr: 27 Type: CL0 Supervision: 
Adr: 4 Type: Duty Supervision: 
Area: 7NT_ Rm: Rm_09 Adr: 28 Type: CL41 - Issues Supervision: 
Adr: 2 Type: Bed1P - OFFLINE Supervision: 
Adr: 3 Type: Bed2 - FB-module missing Supervision: 
Adr: 6 Type: PCAudio Supervision: 
Adr: 7 Type: PC Supervision: 
Adr: 12 Type: PB2C Supervision: 
Adr: 15 Type: Jack2 Supervision: 
Area: 7NT_ Rm: Rm_12 Adr: 29 Type: CL41 - Issues Supervision: 
Adr: 2 Type: Bed1P - OFFLINE Supervision: 
Adr: 3 Type: Bed2 - FB-module missing Supervision: 
Adr: 6 Type: PCAudio Supervision: 
Adr: 7 Type: PC Supervision: 
Adr: 12 Type: PB2C Supervision: 
Adr: 15 Type: Jack2 Supervision: 
Area: 7NT_ Rm: Rm_07 Adr: 30 Type: CL41 - Issues Supervision: 
Adr: 2 Type: Bed1P - OFFLINE Supervision: 
Adr: 3 Type: Bed2 - FB-module missing Supervision: 
Adr: 6 Type: PCAudio Supervision: 
Adr: 7 Type: PC Supervision: 
Adr: 12 Type: PB2C Supervision: 
Adr: 15 Type: Jack2 Supervision: 
Area: 7NT_ Rm: Rm_10 Adr: 31 Type: CL41 - Issues Supervision: 
Adr: 2 Type: Bed1P - OFFLINE Supervision: 
Adr: 3 Type: Bed2 - FB-module missing Supervision: 
Adr: 6 Type: PCAudio Supervision: 
Adr: 7 Type: PC Supervision: 
Adr: 12 Type: PB2C Supervision: 
Adr: 15 Type: Jack2 Supervision: 
Area: 7NT_ Rm: Med_7108 Adr: 32 Type: CL0 Supervision: 
Adr: 4 Type: Duty Supervision: 

For the snippet above, what I would want powershell to output to a file is:

Area: 7NT_ Rm: RM_11 Adr: 26 Type: CL41 - Issues Supervision: 
Adr: 2 Type: Bed1P - OFFLINE Supervision: 
Adr: 3 Type: Bed2 - FB-module missing Supervision: 

Area: 7NT_ Rm: Rm_09 Adr: 28 Type: CL41 - Issues Supervision: 
Adr: 2 Type: Bed1P - OFFLINE Supervision: 
Adr: 3 Type: Bed2 - FB-module missing Supervision: 

Area: 7NT_ Rm: Rm_12 Adr: 29 Type: CL41 - Issues Supervision: 
Adr: 2 Type: Bed1P - OFFLINE Supervision: 
Adr: 3 Type: Bed2 - FB-module missing Supervision: 

Area: 7NT_ Rm: Rm_07 Adr: 30 Type: CL41 - Issues Supervision: 
Adr: 2 Type: Bed1P - OFFLINE Supervision: 
Adr: 3 Type: Bed2 - FB-module missing Supervision: 
 
Area: 7NT_ Rm: Rm_10 Adr: 31 Type: CL41 - Issues Supervision: 
Adr: 2 Type: Bed1P - OFFLINE Supervision: 
Adr: 3 Type: Bed2 - FB-module missing Supervision:

Whenever a line starting with Adr contains one of the phrases I'm looking for (offline, missing, internal, configuration) I would want that line displayed AND the first line starting with Area above the Adr line. If the Area line is identical for multiple Adr lines, then I would only want to display it once.

In the source HTML file that I create the text file from, the Area lines are always tagged with h4 and the Adr lines are always tagged with h5, so from an HTML perspective, if an h5 line contains one of the keywords, I would want to export that to the text file, along with the first h4 line preceding it.

1
  • 1
    please show a reasonably complete set of lines ... and what you want from those lines. try to include enuf of the real thing to allow testing the variants. [grin]
    – Lee_Dailey
    Sep 28, 2020 at 20:30

1 Answer 1

0

You can use this:

((gc "filepath.txt")|select-string -pattern "^Area") >>"output.txt"
([array]::reverse((gc "filepath.txt"))|select-string -pattern ".*OFFLINE.*") >>"output.txt"
8
  • that doesn't seem to grab the lines from the last offilne backwards to the 1st area line.
    – Lee_Dailey
    Sep 28, 2020 at 16:03
  • This would miss the line Adr: 3 Type: Bed2 - FB-module missing Supervision: in the example, and if there are multiple listings of this type in a single file, it will grab the Area line from all of them, regardless of whether there are any OFFLINEs before the next Area. Sep 28, 2020 at 16:03
  • The reason it will miss the indicated line in the example is because Get-Content returns an array of lines, not raw content with actual newline characters in it. Sep 28, 2020 at 16:10
  • Edit: I'll just update my original post so I can add more detail Sep 28, 2020 at 16:21
  • @Lee_Dailey edited it
    – Wasif
    Sep 28, 2020 at 16:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .