Here's an example of extracting a table from an HTML document and turning the table into a series of PSCustomObjects. The HTML document is the one you get in the email body.
# create a phony user
$uc = [PSCustomObject]@{
DisplayName = "DispName"
SamAccountName = "accountname"
UserPrincipalname = "UPN"
City = "Paris"
Co = "fr"
ExtensionAttribute5 = "someattributevalue"
}
$a =
@"
<style>
BODY{background-color:white;}
TABLE{border-width: 3px;border-style: solid;border-color: black;border-collapse: collapse;border-spacing: 15px;width:70%;}
TH{border-width: 2px;padding: 8px;border-style: solid;border-color: black;background-color:#d5ebf5;text-align:center;}
TD{border-width: 2px;padding: 8px;border-style: solid;border-color: black;background-color:#f8faf6;text-align:center;}
</style>
"@
$pre =
@"
<h3>Please Remove the BYE String Alone for the below users from ExtensionAttrbute 5 </h3></br>
"@
# combine all that into an HTML document
$b = $uc | ConvertTo-Html -head $a -PreContent $pre
$h = $b -join "" # Get the HTML into a single string
# Parse the HTML
$HTML = New-Object -Com "HTMLFile"
try {
$html.IHTMLDocument2_write($h)
}
catch {
$encoded = [System.Text.Encoding]::Unicode.GetBytes($h)
$html.write($encoded)
}
# Extract the one (and only) table lement from the HTML
$tables = $html.getElementsByTagName("TABLE")
$table = $tables[0] # NOTE: there's only ONE table in the HTML doc
$titles = @()
$rows = @($table.Rows)
## Go through all of the rows in the table (which includes a header row, if one is present)
foreach($row in $rows)
{
$cells = @($row.Cells)
## If we've found a table header, remember its titles
if($cells[0].tagName -eq "TH")
{
$titles = @($cells | ForEach-Object { ("" + $_.InnerText).Trim() })
continue
}
## If we haven't found any table headers, make up names "P1", "P2", etc.
if(-not $titles)
{
$titles = @(1..($cells.Count + 2) | ForEach-Object { "P$_" })
}
## Now go through the cells in the the row. For each, try to find the
## title that represents that column and create a hashtable mapping those
## titles to content
$resultObject = [Ordered] @{}
for($counter = 0; $counter -lt $cells.Count; $counter++)
{
$title = $titles[$counter]
if(-not $title) { continue }
$resultObject[$title] = ("" + $cells[$counter].InnerText).Trim()
}
## And finally cast that hashtable to a PSCustomObject
[PSCustomObject] $resultObject
}
I'm anticipating your next question to be how to read the email and get the message body. For that, create a NEW post.
BUT . . . if you know how the e-mail is constructed, WHY can't you simple have the script that creates the e-mail create a CSV file instead? That's a lot easier to deal with than what you'll go through to get at the message content!