A DHCP server is a critical component of the IT infrastructure in most environments. In addition to monitoring, which ensures the basic availability of the service, you may also want to check the status of the IP scopes or leases. PowerShell provides several cmdlets for this purpose.

The commands for managing Windows-based DHCP servers are part of the DhcpServer module, which can be obtained by installing the RSAT for DHCP. The cmdlets mentioned here are particularly useful for analyzing problems that arise.

Identifying all servers and querying their settings ^

The first step is to retrieve a list of servers that offer the DHCP service to the network. Of course, this is especially important for larger environments with several DHCP servers. The Get-DhcpServerInDC cmdlet will do the job and is usually invoked without parameters.

However, for further use in other commands, you can store the names of the returned servers in a variable:

$dhcps = (Get-DhcpServerInDC).DnsName

As the name suggests, the cmdlet returns a list of DHCP servers registered in Active Directory. This means that you will not find rogue DHCP servers that were connected to the network without the knowledge of the IT department.

Some basic settings of a DHCP server, such as whether it is a member of the domain and is authorized, can be retrieved with the following query:

Get-DhcpServerSetting -ComputerName <MyDHCP>

This cmdlet will also tell you whether the data has been restored from a backup. This and other information about the database can also be obtained from the following query:

Get-DhcpServerDatabase -ComputerName <MyDHCP>

With

Get-DhcpServerAuditLog -ComputerName <MyDHCP>

you can find out whether logging has been activated for the DHCP service and where the log file is stored. However, you cannot display the log content with this command. Instead, you have to evaluate this text file yourself.

Dedicated cmdlets for IPv4 and IPv6 ^

Most of the module's functions are available in two versions. The functions all have a '4' or a '6' in their names because they relate to IPv4 or IPv6, respectively. The following examples for IPv4 can therefore be applied to IPv6:

To get statistics on the DHCP service for IPv4 as a whole, use:

Get-DhcpServerv4Statistics -ComputerName <MyDHCP>

The statistics contain numbers that you can also get by evaluating individual scopes, but here are the totals for all scopes.

Getting IPv4-related statistics for a DHCP server using Get-DhcpServerv4Statistics

Analysis of scopes ^

Most queries, whether they are about available IP addresses, assigned leases, or general statistics, refer to specific scopes. You can enumerate them using Get-DhcpServerv4Scope. If, for example, you want to output all scopes of all DHCP servers to see whether there is an overlap between them, you could proceed as follows:

$dhcps = (Get-DhcpServerInDC).DnsName
$dhcps | foreach {Get-DhcpServerv4Scope -ComputerName $_}

To get an overview of the most important figures for IPv4 scopes of a DHCP server, use the following:

Get-DhcpServerv4ScopeStatistics -ComputerName <MyDHCP>

This command tells you the percentage of a scope that is already being used and how many addresses are already taken or are still available.

Output statistics for the individual IPv4 scopes

Output statistics for the individual IPv4 scopes

Free, assigned, and reserved addresses ^

The data for free addresses can be obtained with Get-DhcpServerv4FreeIPAddress. If you retrieve it just using the ScopeID parameter, you will only get the next free address. To fetch more available addresses, you have to enter a value for NumAddress:

DhcpServerv4FreeIPAddress -ComputerName <MyDHCP> -ScopeId 192.168.0.0 `
-StartAddress 192.168.0.59 -NumAddress 20

This example lists 20 free addresses, starting from 192.168.0.59.

Outputting free addresses of a scope

Outputting free addresses of a scope

To complement this, you might like to know which addresses have already been assigned to devices:

Get-DhcpServerv4Lease -ComputerName <MyDHCP> -ScopeId 192.168.0.0

In the output of this function, you will find the MAC address of the clients and the status of the leases as additional information.

Displaying issued leases of a scope

Displaying issued leases of a scope

In addition to the free and already assigned addresses, there are usually also addresses that have been reserved for devices with a fixed IP. These can be queried as follows:

Get-DhcpServerv4Reservation -ComputerName <MyDHCP> -ScopeId 192.168.0.0

If a client isn't able to receive an address, you can check to see whether it appears on the deny list:

Subscribe to 4sysops newsletter!

Get-DhcpServerv4Filter -ComputerName <MyDHCP>

The module DhcpServer contains a total of 121 commands that not only query the settings and their status but can also configure the service. The complete documentation can be found on Microsoft Docs.

+5
avataravatar
1 Comment
  1. JC Moriaud 1 year ago

    Very good article thanks.

    Can I add that the command to  to load PoSh DHCPServer module is : 

    Add-WindowsFeature -Name DHCP –IncludeManagementTools

    +1

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account