Skip to content

Commit

Permalink
Create Get-Mac.ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
DXPetti authored Aug 22, 2018
1 parent b5e5000 commit ce13b07
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Get-Mac.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<#
.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"}
}
}

0 comments on commit ce13b07

Please sign in to comment.