Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#Requires -Module DHCPServer
Function Get-UnmatchedDhcpServerv4Reservation {
<#
.Synopsis
A function gets DHCP IPv4 reservations from a single scope stretched over two DHCP servers.
.Description
A function gets DHCP IPv4 reservations from a single scope stretched over two DHCP servers
.Parameter ReferenceComputerName
A DHCP server computer name hosting the ScopeId specified. This parameter is mandatory.
.Parameter DifferenceComputerName
A DHCP server computer name hosting the ScopeId specified. This parameter is mandatory.
.Parameter ScopeId
The ScopeID to check over the two servers to make sure that the reservations are equal. This parameter is mandatory.
.Parameter IncludeEqual
This switch parameter specifies whether to return results that exist on both DHCP servers and are considered equal.
.Example
Get-UnmatchedDhcpServerv4Reservation -ReferenceComputerName DHCP-Server-1 -DifferenceComputerName DHCP-Server-2 -ScopeId 10.0.0.0
Returns reservation objects (like the one below) that are not specified in both DHCP servers:
MissingOnComputer : DHCP-Server-2
Type : Dhcp
ComputerName : DHCP-Server-1
Description :
ScopeId : 10.0.0.0
Name : ExamplePC.whiteways.tech
ClientId : 00-01-23-45-67-89
SideIndicator : <=
IPAddress : 10.0.0.6
.Notes
Author: Information Services
#>
[CmdletBinding()]
Param(
[parameter(
Mandatory = $True,
Position = 1
)]
[string]$ReferenceComputerName,
[parameter(
Mandatory = $True,
Position = 2
)]
[string]$DifferenceComputerName,
[parameter(
Mandatory = $True,
Position = 3
)]
[string[]]$ScopeId,
[parameter(
Position = 4
)]
[switch]$IncludeEqual
)
foreach ($Scope in $ScopeId) {
$ReferenceScope = Get-DhcpServerv4Scope -ComputerName $ReferenceComputerName -ScopeId $Scope -ErrorAction Stop
$DifferenceScope = Get-DhcpServerv4Scope -ComputerName $DifferenceComputerName -ScopeId $Scope -ErrorAction Stop
If ($ReferenceScope -and $DifferenceScope)
{
$Referencev4Reservation = Get-DhcpServerv4Reservation -ScopeId $ReferenceScope.ScopeId -ComputerName $ReferenceComputerName -ErrorAction Stop
$Differencev4Reservation = Get-DhcpServerv4Reservation -ScopeId $DifferenceScope.ScopeId -ComputerName $DifferenceComputerName -ErrorAction Stop
$Placeholder = @{
IPAddress = $null
ScopeId = $null
ClientId = $null
Name = $null
Type = $null
Description = $null
}
if (-not $Referencev4Reservation) {
$Referencev4Reservation = New-Object -TypeName PSObject -Property $Placeholder
}
if (-not $Differencev4Reservation) {
$Differencev4Reservation = New-Object -TypeName PSObject -Property $Placeholder
}
if ($IncludeEqual) {
$Comparison = Compare-Object -ReferenceObject $Referencev4Reservation -DifferenceObject $Differencev4Reservation -Property ClientId, IPAddress -IncludeEqual
}
else {
$Comparison = Compare-Object -ReferenceObject $Referencev4Reservation -DifferenceObject $Differencev4Reservation -Property ClientId, IPAddress, Name, Type, Description
}
foreach ($Compare in $Comparison) {
if (-not $Compare.ClientId) {
continue
}
$ComparisonObject = @{
ComputerName = $null
MissingOnComputer = $null
IPAddress = $Compare.IPAddress
ScopeId = $Scope
ClientId = $Compare.ClientId
Name = $Compare.Name
Type = $Compare.Type
Description = $Compare.Description
SideIndicator = $Compare.SideIndicator
}
if ($Compare.SideIndicator -eq '<=') {
$ComparisonObject.ComputerName = $ReferenceComputerName
$ComparisonObject.MissingOnComputer = $DifferenceComputerName
}
elseif ($Compare.SideIndicator -eq '=>') {
$ComparisonObject.ComputerName = $DifferenceComputerName
$ComparisonObject.MissingOnComputer = $ReferenceComputerName
}
else {
$ComparisonObject.ComputerName = 'Both'
}
New-Object -TypeName PSObject -Property $ComparisonObject
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment