Created
August 22, 2018 06:46
-
-
Save DXPetti/dc2ac3fa4785e765deeec20e8b8e6c28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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