DHCP scopes: Check their utilization with this PowerShell script

Whether you have implemented Windows DHCP servers or a third-party DHCP server, it is necessary to monitor the utilization of DHCP scopes. While Microsoft provides System Center Operations Manager and necessary management packs to monitor all aspects of DHCP servers including monitoring of DHCP scopes, the System Center suite is very expensive and it requires a considerable amount of time to implement all of its components just to enable monitoring of a few servers. In this article, we learn how we can use simple PowerShell commands and a PowerShell script to check the utilization of DHCP scopes on multiple DHCP servers.

Problems if DHCP scopes are not monitored

The DHCP service is one of the components many administrators configure and forget. You might run out of IP addresses resulting in a declining distribution of IP addresses to clients. You must always monitor DHCP scopes to ensure you have plenty of IP addresses available to serve clients. Another important reason for monitoring DHCP scopes is that the overall function of a DHCP server is to distribute IP addresses to clients so clients can communicate over the network. Finally, your objective as an administrator is to always look for ways to decrease downtime of DHCP services and detect security risks.

Monitoring DHCP scopes on a single DHCP Server

Microsoft provides the Get-DhcpServerv4ScopeStatistics PowerShell cmdlet to provide monitoring data for all scopes on a DHCP server. To get monitoring data for all scopes on a DHCP server, simply execute this PowerShell command:

$ThisServerNow = DHCPServer1.TechGenix.com
$results=Get-DhcpServerv4ScopeStatistics -ComputerName $ThisServerNow | Select ScopeID, AddressesFree,AddressesInUse,PercentageInUse,ReservedAddress

The command lists all scopes hosted on DHCPServer.TechGenix.com and provides information such as scope ID, addresses free, addresses in use and percentage in use.

Collecting DHCP scopes monitoring results from multiple DHCP servers

To collect DHCP scopes monitoring results from multiple DHCP Servers, you will be required to use the ForEach loop and also mention all DHCP computer names in a text file used by the PowerShell script explained in the later section of this article.

Requirements

Before you can run the PowerShell script provided in the later section of this article, meet the requirements listed below:

  • You have installed DHCP PowerShell cmdlets on the computer from where you plan to run the script.
  • You have collected all DHCP server names or IP addresses to be checked by the script and specified in the C:\Temp\DHCPServers.TXT file.
  • All DHCP servers specified in C:\Temp\DHCPServers.TXT file are reachable from the computer from where you plan to run the script.
  • Your currently logged on account has permissions to access properties of DHCP servers.

What does the script do?

The PowerShell script performs the following operations:

  • Imports DHCP server PowerShell cmdlets into the current PowerShell session.
  • Checks all DHCP servers mentioned in the C:\Temp\DHCPServers.TXT file.
  • Connects to each DHCP server using Get-DhcpServerv4ScopeStatistics and collects the DHCP scopes monitoring data.
  • Checks the DHCP scopes monitoring data for each scope and appends into C:\Temp\DHCPScopesStatus.CSV file.

The PowerShell script

 
$LocAppDataNow = "C:\Users\Public"
$CurrentLoc="C:\Temp\"
$UniqueTest="EXCH"
 
$TestCSVFile="C:\Temp\DHCPScopesStatus.CSV"
Remove-Item $TestCSVFile -ErrorAction SilentlyContinue
 
$ThisString="DHCP Server, Scope ID, Percentage Used, Final Status"
Add-Content "$TestCSVFile" $ThisString
 
$DHCPServerFile = "C:\Temp\DHCPServers.TXT"
 
$AnyGap = "No"
 
ForEach ($Server in GC $DHCPServerFile)
{
$ThisServerNow = $Server
$TotScopeNotInUse = 0
$TotScopeInUse = 0
 
$Error.Clear()
$results=Get-DhcpServerv4ScopeStatistics -ComputerName $ThisServerNow | Select ScopeID,AddressesFree,AddressesInUse,PercentageInUse,ReservedAddress
IF ($Error.Count -eq 0)
{
$TotScopeNow = $results.Name.Count
 
ForEach ($EachScope in $Results)
{
IF ($EachScope.PercentageInUse -gt 80)
{
$AnyGap = "Yes"
$STR = $ThisServerNow+","+$EachScope.ScopeID+","+$EachScope.PercentageInUse+",Not Ok"
Add-Content $TestCSVFile $STR
}
else
{
$STR = $ThisServerNow+","+$EachScope.ScopeID+","+$EachScope.PercentageInUse+",Ok"
Add-Content $TestCSVFile $STR
}
}
}
else
{
$STR = $ThisServerNow+", Error Connecting to DHCP Server"
Add-Content $TestCSVFile $STR
}
}
 
IF ($AnyGap -eq "Yes")
{
$TestStatus="High"
$TestText="HIGH ISSUE"
}
else
{
$TestStatus="Passed"
$TestText="MEDIUM ISSUE"
}

Once the script has finished executing, the output will appear in the C:\Temp\DHCPScopesStatus.CSV file. As you can see in the output generated below, the script checked seven DHCP Servers hosted in TechGenix.com domain and two DHCP servers have two scopes hosted which have been used 85 percent and 90 percent respectively. As you can see in the Final Status column also shows “Not OK” for these two scopes.

DHCP Scopes

Important: There are many other DHCP checks you should perform to keep DHCP servers healthy and operational. The above PowerShell script was obtained from DynamicPacks IT Health Profiler Version 5.4.1.1, which is capable of doing a complete health assessment of DHCP servers and provide a report that contains recommendations to fix the DHCP-related issues. In DynamicPacks IT Profiler, you can see results for all DHCP items in a single console as shown in the screenshot below, which is taken from IT Profiler Summary console:

DHCP Scopes

Now you know why it is important to monitor the DHCP scopes. We provided some PowerShell commands to check DHCP scopes on a single DHCP server and a PowerShell script to monitor DHCP scopes on multiple DHCP servers.

Featured image: Shutterstock

Leave a Comment

Your email address will not be published.

Scroll to Top