1

We receive a email every day that shows us what has pass or failed its backup. What I have done is extract the HTMLbody of the email and placed in into its own html file body.html.

The body.html is like the following 11 headings
Server, Client, Status, Group, Job, Size Scanned (TB), Started, Finished, Duration (hour), Retention (week), Expires

Under each heading it has data. enter image description here

I want to be able to search the html body.html for the word "failed" (which will be only located under the Status column and return the whole line.

I have tried to grab it into a variable and then do a search on it. But it returns all the code.

Any ideas ?

$Outlook = New-Object -ComObject Outlook.Application
$OutlookFolders = $Outlook.Session.Folders.Item(1).Folders

$OutlookInbox = $Outlook.session.GetDefaultFolder(6)

$latestmail=$OutlookInbox.items | select -last 1
$emailhtmlbody=$latestmail.HTMLBody

$emailhtmlbody | out-file d:\delme\test\body.html
goced
  • 61
  • 2
  • 9
  • If the HTML is small, are you able to provide it for testing? Be sure to sanitise the code first so nothing sensitive is contained. – Drew Jun 07 '19 at 04:38
  • The table is big . It has like over 230 lines. I need to work out how to add it to the question – goced Jun 07 '19 at 04:41
  • Yea, don't add that to the question. But you are saying it is a table so that changes how it is approached. Is each row of the table on a new line (in the raw HTML)? – Drew Jun 07 '19 at 04:43
  • You need to show us a minimized part of the `$emailhtmlbody`, so we know if it is possible to iteratie line by line or if the contents is compressed (no newlines) format. The image shows only green lines (success), but you are interested in `failed` status. Could be these rows have a different class to search on, but we cannot see that now. – Theo Jun 07 '19 at 11:31
  • If `$emailbody` contains valid html, you could examine it either directly or after casting it to `[xml]` This [answer](https://stackoverflow.com/a/31949668/6811411) from JohnLBevan to the question [how-to-convert-html-table-to-csv-file-with-same-structure-with-powershell](https://stackoverflow.com/questions/25918094/how-to-convert-html-table-to-csv-file-with-same-structure-with-powershell) looks promising. –  Jun 07 '19 at 13:35
  • I had an idea. I think a easy way to do it is to use regex .The query would be to search the $emailhtmlbody for "......black">failed .... " And output that to a html file. It will contain all the data and it will already be formatted in html . Working on the regex search query – goced Jun 09 '19 at 22:45

1 Answers1

0

You could iterate over the body line by line, match lines against a regular expression and capture the blocks you're interested in. Here is an example:

foreach ($line in $emailhtmlbody) { 
    if ($line -match "<td>(?<Server>.+)</td><td>(?<Client>.+)</td><td>(?<Status>.+)</td>...") {
        [PSCustomObject] @{
            Server = $Match.Server
            Client = $Match.Client
            Status = $Match.Status
        }
    }
}

There are certainly some id or class descriptors in the HTML that you can use to only match the lines you're interested in.

I would first build a working regular expression in a tool such as RegexCoach. These can become quite complex.

Olivier Boudry
  • 801
  • 7
  • 17
  • Ok I did something a little different $Outlook = New-Object -ComObject Outlook.Application $OutlookFolders = $Outlook.Session.Folders.Item(1).Folders $OutlookInbox = $Outlook.session.GetDefaultFolder(6) $latestmail=$OutlookInbox.items | select -last 1 $emailhtmlbody=$latestmail.HTMLBody – goced Jun 10 '19 at 02:18
  • #split the $emailhtmlbody data to a new line based on finding <*tr> $FaildSearch = $emailhtmlbody -split "<.?tr>" | where {$_ -like "*failed*"} New-Item D:\delme\test\backupfailed.html -type file Add-Content D:\delme\test\backupfailed.html "" Add-Content D:\delme\test\backupfailed.html $FaildSearch Add-Content D:\delme\test\backupfailed.html "" – goced Jun 10 '19 at 02:20