PowerShell fun with Select-String

Hi Everyone!
It has been a while so I wanted to chime in on some PowerShell fun.
In this particular problem I have a SharePoint email enabled list that receives trial requests. On the server runs a PowerShell script that loads up the list, and parses out the raw email messages that are attached.
When the script was originally written it keyed off specific lines in the message to gleam the relevant data by line count. At first this was a good idea, but a problem arose with the raw email messages. Each raw email contains an email header tracing the specific route an email took going from point A to point B. Most of the time it was not a problem, but this assumed that the route would never change.
Well things changed not once, but multiple times. Looking at the original script I wanted something I could inject into the script that wouldn’t require a massive rewrite of the script.
I finally settled on searching for a specific term in the email that was after the variable length email headers section. In this case the term is “Field12” and from that point all the other points of information were always fixed lines down from that point.

$lineLocation = Select-String -path c:\scripts\tempFile.txt -pattern “id=”Field12″”
$lineLocation1 = $lineLocation.LineNumber
$company = $emailFile[$lineLocation1]
$emailAddress = $emailFile[$lineLocation1 + 27]
$edition = $emailFile[$lineLocation1 + 35]

There are of course more elegant ways to approach this same problem, but I wasn’t looking to rewrite the the entire script. I hope this helps you…

Tags :

About the Author

Leave a Reply

%d bloggers like this: