I have a function called "Send-Email" which generates a htm file which is then sent out via email using the Send-MailMessage cmdlet
The htm file is created using the Add-Content cmdlet
Code: Select all
function Send-Email
{
$date = Get-Date -Format f
$agent = Get-ADUser -Identity $CurrentUser -Properties DisplayName | Select-Object DisplayName -ExpandProperty DisplayName
$UserDescription = Get-ADUser $ADUser -Properties Description | Select-Object Description -ExpandProperty Description
$Report = New-Item -Path ("$logs\$ADUser.htm") -Type File
#Builds the headers and formatting for the report
Add-Content $report "<html>"
Add-Content $report "<head>"
Add-Content $report "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $report '<title>Disabled User Alert</title>'
Add-content $report '<STYLE TYPE="text/css">'
Add-content $report "<!--"
Add-content $report "td {"
Add-content $report "font-family: Verdana;"
Add-content $report "font-size: 11px;"
Add-content $report "border-top: 1px solid #999999;"
Add-content $report "border-right: 1px solid #999999;"
Add-content $report "border-bottom: 1px solid #999999;"
Add-content $report "border-left: 1px solid #999999;"
Add-content $report "pAdding-top: 0px;"
Add-content $report "pAdding-right: 0px;"
Add-content $report "pAdding-bottom: 0px;"
Add-content $report "pAdding-left: 0px;"
Add-content $report "}"
Add-content $report "body {"
Add-content $report "margin-left: 5px;"
Add-content $report "margin-top: 5px;"
Add-content $report "margin-right: 0px;"
Add-content $report "margin-bottom: 10px;"
Add-content $report ""
Add-content $report "table {"
Add-content $report "border: thin solid #000000;"
Add-content $report "}"
Add-content $report "-->"
Add-content $report "</style>"
Add-Content $report "</head>"
Add-Content $report "<body>"
$date.ToString()
Add-content $report "<table width='100%'>"
Add-content $report "<tr bgcolor='#CCCCCC'>"
Add-content $report "<td colspan='7' height='25' align='center'>"
Add-content $report "<font face='Verdana' color='#41B9B9' size='4'><strong>The following user has just been disabled by $agent</strong></font>"
Add-content $report "</td>"
Add-content $report "</tr>"
Add-content $report "</table>"
Add-content $report "<table width='100%'>"
Add-Content $report "<tr bgcolor=#CCCCCC>"
Add-Content $report "<td width='20%' align='left'>Account Name</td>"
Add-Content $report "<td width='10%' align='center'>Disabled Date</td>"
Add-Content $report "<td width='50%' align='center'>Description</td>"
Add-Content $report "</tr>"
Add-Content $report "<tr>"
Add-Content $report "<td>$ADUser</td>"
Add-Content $report "<td>$date</td>"
Add-Content $report "<td>$UserDescription</td>"
Add-Content $report "</body>"
Add-Content $report "</html>"
#Assembles and sends alert
$emailFrom = "<emailaddress>"
$emailTo = "<emailaddress>"
#$cc = "<emailaddress>"
$subject = "Disabled User Alert"
$smtpServer = "server"
$body = Get-Content $report | Out-String
Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -BodyAsHtml -Body $body -SmtpServer $smtpServer
Cheers