Using PowerShell to Investigate Duplicate IP Address Issues that were issued from a Microsoft based DHCP Server
Over the past couple of weeks, you've received several support calls about duplicate IP Address issues from users whose machines receive their IP Address from your organizations internal Microsoft based DHCP Servers.
The users who reported the issues use thin clients and when the IP Address conflicts occur they receive a popup message containing their IP Address and the MAC Address of the machine that duplicated the IP Address.
You and your staff need to retrieve additional information about the IP Address and MAC Address that the users received in order to try to determine what's causing the problem.
While this information could be obtained from the DHCP Servers using PowerShell one-liners, you've decided to write a function to simplify the process of obtaining this sort of information long term for you and for your staff.
1#Requires -Version 4.0
2#Requires -Modules DhcpServer
3
4function Get-IPDuplicateInfo {
5
6<#
7.SYNOPSIS
8 Retrieves the DHCP Lease Info for a specific IP Address or MAC Address from a Windows based DHCP Server.
9
10.DESCRIPTION
11 Get-IPDuplicateInfo is a function that retrieves information about a specific IP address or MAC Address from
12a Microsoft Windows Server that is running the DHCP Server role.
13
14.PARAMETER IPAddress
15 The IP Address that your attempting to retrieve DHCP information for. This parameter is mandatory and should
16be entered in the following format: 192.168.1.1
17
18.PARAMETER ClientId
19 The Media Access Control (MAC) Address of the network card that you're attempting to retrieve information for.
20The parameter is mandatory and the value for it must be specified in MAC-48 format which is six groups of two
21hexadecimal digits separated by dashes: 01-23-45-67-89-AB.
22
23.EXAMPLE
24 Get-IPDuplicateInfo -IPAddress '192.168.1.1'
25
26.EXAMPLE
27 Get-IPDuplicateInfo -ClientId '01-23-45-67-89-AB'
28
29.INPUTS
30 None
31
32.OUTPUTS
33 DhcpServerv4Lease
34
35.NOTES
36 Created On: February 6, 2014
37 Created By: Mike F Robbins
38 Website: http://mikefrobbins.com/
39 Twitter: @mikefrobbins
40 Contact Email: IgBtAGkAawBlAGYAcgBvAGIAYgBpAG4AcwBAAG0AcwBwAHMAdQBnAC4AYwBvAG0AIgA=
41
42#>
43
44 [CmdletBinding(DefaultParameterSetName='IP Address')]
45 param(
46 [Parameter(Mandatory,
47 ParameterSetName='IP Address')]
48 [ValidateScript({
49 If ($_ -match "^(([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){3}([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])$") {
50 $True
51 }
52 else {
53 Throw "$_ is not a valid IPv4 Address or was not specified in the required format. Enter a valid IP address in the following format: '192.168.1.1'"
54 }
55 })]
56 [string]$IPAddress,
57
58 [Parameter(Mandatory,
59 ParameterSetName='MAC Address')]
60 [ValidateScript({
61 If ($_ -match '^([0-9A-F]{2}[-]){5}([0-9A-F]{2})$') {
62 $True
63 }
64 else {
65 Throw "$_ is either not a valid MAC Address or was not specified in the required format. Please specify one or more MAC addresses in the follwing format: '01-23-45-67-89-AB'"
66 }
67 })]
68 [Alias('MACAddress')]
69 [string]$ClientId,
70
71 [Parameter(ParameterSetName='IP Address')]
72 [Parameter(ParameterSetName='MAC Address')]
73 [Parameter(DontShow)]
74 [ValidateNotNullOrEmpty()]
75 [Alias('ServerName')]
76 [string[]]$ComputerName = (Get-DhcpServerInDC |
77 Select-Object -ExpandProperty DnsName)
78 )
79
80 foreach ($computer in $ComputerName){
81
82 $Params = @{
83 ComputerName = $computer
84 ErrorAction = 'Stop'
85 }
86
87 If ($PSBoundParameters['IPAddress']) {
88 $Params.IPAddress = $IPAddress
89
90 try {
91 Get-DhcpServerv4Lease @Params
92 }
93 catch [Microsoft.Management.Infrastructure.CimException]{
94 Write-Verbose -Message "No DHCP Lease exists on DHCP Server $computer for IPAddress '$IPAddress'."
95 }
96 catch {
97 Write-Warning -Message "An error has occured. Error Details: $_.Exception.Message"
98 }
99
100 }
101
102 elseif ($PSBoundParameters['ClientId']) {
103
104 try {
105 $DHCPScopes = (Get-DhcpServerv4Scope @Params |
106 Select-Object -ExpandProperty ScopeId).IPAddressToString
107 }
108 catch {
109 Write-Warning -Message "Unable to obtain DHCP Scope information from DHCP Server $computer. Error Details: '$_.Exception.Message'."
110 }
111
112 $Params.ClientId = $ClientId
113
114 foreach ($DHCPScope in $DHCPScopes) {
115 $Params.ScopeId = $DHCPScope
116
117 try {
118 Get-DhcpServerv4Lease @Params
119 }
120 catch [Microsoft.Management.Infrastructure.CimException]{
121 Write-Verbose -Message "Lease information for Client ID '$ClientId' not found in DHCP Scope $DHCPScope on DHCP Server $ComputerName."
122 }
123 catch {
124 Write-Warning -Message "An error has occured. Error Details: $_.Exception.Message"
125 }
126 }
127
128 }
129
130 else {
131 Write-Error -Message "An error has occurred. Please contact your system administrator."
132 }
133
134 }
135
136}
Simply provide an IP Address or MAC Address and information about it is returned from the DHCP Server(s):
To receive even more detailed information, pipe the results to the Select-Object cmdlet and select all of the properties:
This function was tested from a Windows 8.1 workstation with the Remote Server Administration Tools (RSAT) installed and it was tested against a Windows Server 2012 R2 server and a Windows Server 2008 R2 server, both with the DHCP Server role installed.
µ