Monday, April 18, 2016

Email DHCP Scope Statistics in an HTML table



I was asked to send an email daily of our DHCP Scope Statistics to our VDI team, so they can plan properly and monitor usage on some scopes where IP addresses are tight. I know...it sounds incredibly exciting and you're all wondering how I sleep at night when I get to do such cool stuff! Anyway, I came up with this script to email the DHCP Scope Statistics on a daily basis. We have monitoring enabled that will alert us if the scopes are running out of IP addresses, but in an effort to be more proactive, they wanted a simple email. Since everyone and their dog emails out Excel attachments every day, I wanted to mix things up a bit and just send the data in HTML, since they were only using this for reference and not actually doing anything with the data.

Script Requirements:
- Powershell v2
- DHCP Servers 2008-2012R2
- DHCP PowerShell Module

Usage: Modify the Send-MailMessage line with appropriate sender, recipient, and SMTP information. There are no other changes that are required, as it will detect DHCP Servers in your domain and report from all Active DHCP Scopes. Simply run the Get-DHCPScopeStats.ps1 script and wait for your email.

Features:
  • Exports DHCP Scope Statistics from all Active DHCP Scopes in the domain
  • Displays Scope Name, Network, DHCP Server, Free IPs, IPs in Use, Start, and End of DHCP Scope.
  • Sends email in HTML format with embedded table for an easy read without opening an attachment.
Sample from Email:

PowerShell
Import-Module DhcpServer 
 
$a = "" 
$a = $a + "BODY{background-color:White;font-family: Arial; font-size: 10pt;}" 
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}" 
$a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:silver}" 
$a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:WhiteSmoke}" 
$a = $a + "" 
 
$DHCPStats = @() 
$DHCPStatsString = @() 
 
show-dhcpservers|foreach-object { 
$DHCPSrvr = $_.Server 
Get-DhcpServerv4Scope -ComputerName $_.Server|where {$_.State -eq 'Active'}|ForEach-Object { 
$ScopeName = $_.Name 
$ScopeID = $_.ScopeID 
$SM = $_.SubnetMask 
$Start = $_.StartRange 
$End = $_.EndRange 
$Lease = $_.LeaseDuration 
$State = $_.State 
$IPFree = (Get-DhcpServerv4ScopeStatistics -ComputerName $DHCPSrvr -ScopeID $ScopeID).Free 
$IPInuse = (Get-DhcpServerv4ScopeStatistics -ComputerName $DHCPSrvr -ScopeID $ScopeID).Inuse 
$DHCPStats = New-Object -TypeName PSObject 
$DHCPStats | Add-Member -MemberType NoteProperty -Name ScopeName -Value $ScopeName 
$DHCPStats | Add-Member -MemberType NoteProperty -Name ScopeNetwork -Value $ScopeID 
$DHCPStats | Add-Member -MemberType NoteProperty -Name FreeIPs -Value $IPFree 
$DHCPStats | Add-Member -MemberType NoteProperty -Name DHCPServer -Value $DHCPSrvr 
$DHCPStats | Add-Member -MemberType NoteProperty -Name IPsInUse -Value $IPInuse 
$DHCPStats | Add-Member -MemberType NoteProperty -Name Start -Value $Start 
$DHCPStats | Add-Member -MemberType NoteProperty -Name End -Value $End 
$DHCPStatsString +$DHCPStats 
} 
} 
$DHCPStatsBody = $DHCPStatsString|Select ScopeName, ScopeNetwork, DHCPServer, FreeIPs, IPsInUse, Start, End|Sort-object FreeIPs|ConvertTo-HTML -Head $a -Body "<H2>DHCP Server Statistics</H2>" 
$body = "<font face='arial'> " 
$body +"`n" 
$body +$DHCPStatsBody 
$body +"`n" 
 
Send-MailMessage -From "SourceEmail@contoso.com" -To "Recipient@contoso.com" -Subject "DHCP Scope Statistics" -Body "$body" -BodyAsHtml -SmtpServer "smtp.contoso.com"