Author Posts

February 5, 2017 at 5:34 pm

Hey All

Currently I have ran into a problem with the way my results are reading when sending email containing what was captured.

#Pull in email showing approvals
Function Global:Get-Email {
Param(
[String]$Folder = "InBox",
[String]$Test ="Subject",
[String]$Compare = "Group Access"
    )
Process{
$Folder = $Folder
Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.GetNameSpace("MAPI")
$NameSpace.Folders.Item(0)
$Email = $NameSpace.Folders.Item(1).Folders.Item($Folder).Items
Clear-Host
Write-Host "Trawling through Outlook, please wait ...."
$Email | Where-Object {$_.$Test -match $Compare} | Sort-Object -Property `
@{Expression = "Unread";Descending=$true}, `
@{Expression = "Subject";Descending=$true} `
| select subject | Out-String
$Output = $Email | Where-Object {$_.$Test -match $Compare} | Sort-Object -Property `
@{Expression = "Unread";Descending=$true}, `
@{Expression = "Subject";Descending=$true} `
| select subject | Out-String
Write-Host "Sending Email containing results"
Send-MailMessage -SmtpServer 'smtp.youremail.com' -To 'youremail@youremail.com'  -From 'youremail@youremail.com' -Subject "Automated Approval Report" -Body "$Output" -BodyAsHtml -Priority High -ErrorAction Stop
    } # End of main section 'Process'
}
Get-Email  #-Compare false

Gives me this Output, which is correct, the email just reads wrong, explained below
_________________________________________
Trawling through Outlook, please wait ....

Subject
——-
Group Access – Order# 0000000 has been APPROVED
Group Access – Order# 0000001 has been APPROVED

However the email that is sent looks like this:
Subject ——- Group Access – Order# 0000000 has been APPROVED Group Access – Order# 0000000 has been APPROVED

I would like it to look like this:
Group Access – Order# 0000000 has been APPROVED
Group Access – Order# 0000001 has been APPROVED

February 5, 2017 at 5:49 pm

That's because you've specified that the body is HTML. In HTML, carriage returns are ignored. The problem is that you haven't actually provided HTML. I'd consider removing -BodyAsHTML. Or, run your output through ConvertTo-HTML (using -Fragment) before Out-String, if HTML is actually your desire.

Also, you should consider switching to Write-Verbose for your "status" messages.

February 5, 2017 at 10:50 pm

Those are some good tips, i will test them out and post my results, still learning so all the help is very appropriated!

February 5, 2017 at 10:56 pm

So i have taken -bodyashtml
My result is now better but i cannot get it to split the string, going to do some research, below is the return i get in email

Subject
——-
Group Access – Order# 2132098 has been APPROVED Group: GG$NSC-ID Administration – User: lentzk1 Group Access – Order# 2132098 has been APPROVED Group: GG$NSC-ID Administration – User: dawsoa2