• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 598
  • Last Modified:

Help with HTML email report generated from a Powershell script

Greetings Powershell Experts,

I have written a small Powershell script (with the help of various Experts here on Experts-Exchange) that basically allows me to input a department in our organization and generate a list of the users in that department along with their mailbox size.  I have decided to try and make the output an HTML report that can be emailed to myself or someone else.  The code for the script is as follows:

$Header = "<style type='text/css'>`n" + `
  "td { width: 200px }`n" + `
  "th { text-align: left }`n" + `
  "</style>"

$Department = Read-Host "Input the department for which you would like to run the pre-migration mailbox report."
$Users = Get-user -Filter {Department -like $Department}
$title = foreach ($user in $Users) {select-object title}

$Body = foreach ($user in $Users) {
Get-MailboxStatistics $User.samaccountname | `
    Select-object `
    @{n="User";e={$_.DisplayName}}, `
    @{n="Job Title";e={$user.title}}, `
    @{n="Mailbox Size (MB)";e={$_.totalitemsize.value.ToMB()}}}
  ConvertTo-HTML -Title "Pre-migration Mailbox Report" -Head $Header


$Mail = New-Object System.Net.Mail.MailMessage( `
  "MailAdmin@domain.com", `
  "nick@domain.com", `
  "PRE-MIGRATION REPORT", `
  $Body)
 
$Mail.IsBodyHTML = $True
 
$Smtp = New-Object System.Net.Mail.SmtpClient("cas-hub01.summitlan.states")
$Smtp.Send($Mail)

PLEASE NOTE:  I DON’T UNDERSTAND THE HTML STUFF AT ALL.  I FOUND A COUPLE EXAMPLES AND PATCHED THIS TOGETHER TO SEE IF I COULD GET IT TO WORK.

When I run this, I get a blank email.  Can someone help me with the HTML part of this?

Thanks,
Nick
0
ndalmolin_13
Asked:
ndalmolin_13
  • 2
1 Solution
 
slidingfoxCommented:
I think i may have replied to another one of your questions. The script below is currently untested, but i've used this idea many times, so if it doesn't work, let me know and I should be able to work out whats wrong.

It doesn't create a HTML email as I've had loads of security warnings from various email clients in the past. instead, it attaches a HTML file.

$head = @'
<style>
    body { 
        background-color:#ffffff;
        font-family:Tahoma;
        font-size:10pt;
    }

    td, th {
        border:1px solid black;
        border-collapse:collapse;
    }

    th {
        color:white;
        background-color:#ff0000;
    }

    table, tr, td, th {
        padding:2px;
        margin:0px
    }   
</style>
'@

$Department = Read-Host "What department do you want the mailbox size report for?"
$Users = Get-user -Filter {Department -like $Department} | Select-Object SamAccountName
$results = @()
foreach ($user in $Users) {
    $mailboxstats = Get-MailboxStatistics $User.samaccountname -WarningAction SilentlyContinue | Select-object @{n="User";e={$_.DisplayName}}, @{n="Mailbox Size (MB)";e={$_.totalitemsize.value.ToMB()}} 
    
    if ($mailboxstats) {
        $results += $mailboxstats
    } else {
        Add-Content "C:\Temp\UsersNotLoggedOnYet.txt" "$user"
    }
}

$fragment = $Results | ConvertTo-Html -Fragment -PreContent '<h3>Pre-Migration report</h3>' | Out-String

$PreContent = @"
<h2>Title for HTML Report</h2>
"@

ConvertTo-Html -head $head -PostContent $fragment1,$fragment2,$fragment3 -PreContent $PreContent | Out-File "C:\Temp\HTMLReport.html"

$Smtp = New-Object System.Net.Mail.SmtpClient("cas-hub01.summitlan.states")
$msg = new-object Net.Mail.MailMessage
$attach = new-object Net.Mail.Attachment("C:\Temp\HTMLReport.html")

$msg.From = "MailAdmin@domain.com"
$msg.To.Add("nick@domain.com")
$msg.Subject = "PRE-MIGRATION REPORT"
$msg.Body = "Please find attached the HTML Report"
$msg.Attachments.Add($attach)

$Smtp.Send($msg)

Open in new window

0
 
dicconbCommented:
Greetings  ndalmolin_13!

The ConvertTo-HTML section isn't quite right.  Try this:

$Header = "<style type='text/css'>`n" + `
  "td { width: 200px }`n" + `
  "th { text-align: left }`n" + `
  "</style>"

$Department = Read-Host "Input the department for which you would like to run the pre-migration mailbox report."
$Users = Get-user -Filter {Department -like $Department} 
$title = foreach ($user in $Users) {select-object title}

$data = foreach ($user in $Users) {
Get-MailboxStatistics $User.samaccountname | `
    Select-object `
    @{n="User";e={$_.DisplayName}}, `
    @{n="Job Title";e={$user.title}}, `
    @{n="Mailbox Size (MB)";e={$_.totalitemsize.value.ToMB()}}} 
$body=$data | ConvertTo-HTML -Title "Pre-migration Mailbox Report" -Head $Header


$Mail = New-Object System.Net.Mail.MailMessage( `
  "MailAdmin@domain.com", `
  "nick@domain.com", `
  "PRE-MIGRATION REPORT", `
  $Body)
 
$Mail.IsBodyHTML = $True
 
$Smtp = New-Object System.Net.Mail.SmtpClient("cas-hub01.summitlan.states")
$Smtp.Send($Mail)

Open in new window


Cheers,

D
0
 
dicconbCommented:
Thanks for the points,

D
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now