Jump to content

Powershell, Regex, Replace and multiple lines.


Recommended Posts

Posted (edited)

We're having to pull off a bunch of emails pertaining to a particular student. We've managed to get every email containing [string]$PupilName easy enough. The problem is there's a lot of them, and each one has a bunch of junk data that needs stripping out.

 

The email logs come off as .msg files which are just text files.

MDaemon stuff begins X- (X-Spam-Processed, X-MDHelo, etcetera) needs to be stripped out (-replace "^X-.*", $null)

Blank lines are written as =20 and need to be replaced (-replace "^=20$", " ")

 

Then follows, for some reason, the entire body of the email all over again, but in HTML. So I need to strip out from . The problem with this is there are line breaks every 76 characters or less.

osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" xmlns=3D"http:=
//www.w3.org/TR/REC-html40"> =3D"text/html; charset=3Dus-ascii"> oft Word 14 (filtered medium)">

$", "null"

[Regex]::Replace("", "$null", @('MultiLine'))

etcetera.

Edited by Garacesh
Posted

I would do a read of every line, if contains

Then check each other line for /html> and if so, trim the string up to that point and set a delete line flag to false. Else, delete line.

 

If you want, I will give a go at typing a script together that will do it when I have a moment.

Posted
I would do a read of every line, if contains

Then check each other line for /html> and if so, trim the string up to that point and set a delete line flag to false. Else, delete line.

 

If you want, I will give a go at typing a script together that will do it when I have a moment.

 

I can get the script examining each message line-by-line by using $(Get-Content P:\ath\to\email.msg)[0] ([1], [2], [0..20] etcetera) which is what I'm investigating now, I figure maybe if I can find that $Email[56] is " I can work around that.

Of course I'll need to examine each email line-by-line, I can't just hard-code the values. But it should be doable.

Posted

Ended up 'solving' it, but it's a bit of a kludge.. Ideally I'd like to clean it up before it's next used (as this situation of "All emails pertaining to $Thing" crops up every now and again)

 

$OutputFile = "D:\Emails"
Get-ChildItem "\\path\to\folder" -Include "*.msg" -Name | ForEach-Object {
   $Email = (Get-Content "\\path\to\folder\$_")
   do {
       [int]$Count = 0
       [int]$HTMLOpen = 0
   } until (($Count -eq 0) -and ($HTMLOpen -eq 0))
   do {
       $Count = ($Count + 1)
       if ($Email[$Count] -imatch "^            $HTMLOpen = $Count
       }
   } until (($HTMLOpen -ne 0) -or ($Count -eq $Email.Length))
   if ($Count -ne $Email.Length) {
       $Email = $Email[0..($HTMLOpen - 5)]
   }
   $Email -replace "^X-.*", $null -replace "^=20$", " " | Out-File $OutputFile\$($_ -replace ".msg", ".txt")
}

 

Really don't like the implementation there, but it got the job done.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...