Powershell Needful Things put that in your pipeline


Windows 2008 R2 DHCP Scope Utilisation Report

Posted by Jean Louw

Just a quick little script I developed for a client this week. As you know Windows 2012 DHCP server has some pretty cool Powershell cmdlets built in, but sadly, in the case of Windows 2008 there are none.

My customer needed to know when scopes are starting to run out of addresses. I found that the DHCP server logs an event each hour after startup for all scopes that are at 80% utilisation or more.

My solution was the following script. The script is triggered by the 1020 event in the system log, and then finds the events for the last hour, and send an email to the guys to alert them.

You need to modify the script for your servers SMTP parameters and if you prefer to run the script remotely you can use -computername on the the get-eventlog cmdlet.

Admittedly, this is not the best way if you have something like SCOM in place, but as a rudimentary solution it does exactly what they need.

$eventList = @()
Get-EventLog -LogName System -After (get-date).AddHours(-1) -Source DhcpServer -InstanceId 1020 `
| foreach-Object {
$row = "" | Select ScopeAddress, Utilization, FreeIPAddresses
$row.ScopeAddress = $_.ReplacementStrings[0]
$row.Utilization= $_.ReplacementStrings[1]
$row.FreeIPAddresses = $_.ReplacementStrings[2]
$eventList += $row

$messageParameters = @{
Subject = "DHCP Scope Utilisation Report - $((Get-Date))"
Body = $eventList | Sort Utilization -Descending |
ConvertTo-Html |
From = "Scope Alert <scopealert@xyz.com>"
To = "group@xyz.com"
SmtpServer = "xxx.xxx.xxx.xxx"

Send-MailMessage @messageParameters -BodyAsHtml
Filed under: powershell, script 3 Comments