Skip to content

Instantly share code, notes, and snippets.

@DXPetti
Created August 22, 2018 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DXPetti/dc2ac3fa4785e765deeec20e8b8e6c28 to your computer and use it in GitHub Desktop.
Save DXPetti/dc2ac3fa4785e765deeec20e8b8e6c28 to your computer and use it in GitHub Desktop.
<#
.Synopsis
Search DHCP for the specified MAC address
.DESCRIPTION
This function enumerates through each scope in either a defined site or the current site and displays any DHCP lease or reservation that matches the MAC address specified
.EXAMPLE
Get-Mac -Mac 000000000000
.EXAMPLE
Get-Mac -Mac 0000 -DhcpSite CONTOSO
.EXAMPLE
Get-Mac -Mac 000000 -DhcpSite *
#>
function Get-Mac
{
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
$Mac,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
Position=1)]
$DhcpSite
)
Begin
{
$MacAddress = $Mac -replace '..(?!$)', '$&-'
$Leases = @()
$DhcpServers = Get-DhcpServerInDC | Where-Object {$_.DnsName -like "$DhcpSite*"}
}
Process
{
$i = 1
foreach ($DhcpServer in $DhcpServers.DnsName)
{
Write-Progress -Id 1 -Activity "Gathering DHCP Scopes" -Status "from $DhcpServer" -PercentComplete ($i++ / $DhcpServers.Count * 100)
$DhcpScopes = Get-DhcpServerv4Scope -ComputerName $DhcpServer
$j = 1
foreach ($DhcpScope in $DhcpScopes.ScopeId)
{
Write-Progress -ParentId 1 -Activity "Gathering DHCP Leases" -Status "from $DhcpScope" -PercentComplete ($j++ / $DhcpScopes.Count * 100)
$Leases += Get-DhcpServerv4Lease -ComputerName $DhcpServer -ScopeId $DhcpScope
}
}
}
End
{
Write-Host "Found MAC Address $MacAddress in the following locations:"
$Leases | Where-Object {$_.ClientId -Match "$MacAddress"}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment