Link to home
Start Free TrialLog in
Avatar of ndalmolin_13
ndalmolin_13Flag for United States of America

asked on

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
Avatar of slidingfox
slidingfox
Flag of United Kingdom of Great Britain and Northern Ireland image

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

ASKER CERTIFIED SOLUTION
Avatar of dicconb
dicconb
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the points,

D