Skip to content

Instantly share code, notes, and snippets.

@bharkr
Last active October 2, 2023 19:18
Show Gist options
  • Save bharkr/026738f3cdd4fb3e2c46b6196c1524db to your computer and use it in GitHub Desktop.
Save bharkr/026738f3cdd4fb3e2c46b6196c1524db to your computer and use it in GitHub Desktop.
Get-DhcpLeaseInfo
function Get-DhcpLeaseInfo {
[CmdletBinding(DefaultParameterSetName='HostName')]
param(
[Parameter(Mandatory=$true, HelpMessage='Name of the the DHCP server being queried.')]
[string]$ComputerName,
[Parameter(ParameterSetName='HostName', HelpMessage='The host name property to be queried.', Mandatory=$true)]
[string]$HostName,
[Parameter(ParameterSetName='OUI', Mandatory=$true,
HelpMessage='First three octets of the mac addresses you being searched for. For example: AA-BB-CC')]
[string]$OUI,
[Parameter(ParameterSetName='IP', Mandatory=$true, HelpMessage='IP Address of the client being searched for.')]
[ValidatePattern('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')][string]$IPAddress
)
switch ($PSCmdlet.ParameterSetName) {
'HostName' {
Get-DhcpServerv4Scope -ComputerName $ComputerName | ForEach-Object {
Get-DhcpServerv4Lease -ComputerName $ComputerName -AllLeases -ScopeId ($_.ScopeID)
} | Where-Object 'HostName' -Match $HostName
}
'OUI' {
$regex = "^([0-9A-Fa-f]{2}[:-]){2}[0-9A-Fa-f]{2}$"
if ($OUI -match ':') {
$OUI = $OUI.Replace(':','-')
}
if ($OUI -match $regex){
Get-DhcpServerv4Scope -ComputerName $ComputerName | ForEach-Object {
Get-DhcpServerv4Lease -ComputerName $ComputerName -AllLeases -ScopeId ($_.ScopeID)
} | Where-Object 'clientid' -Match $OUI
}
else {
Write-Error 'The OUI input is not an appropriately formatted mac address'
}
}
'IP' {
if ([ipaddress]$IPAddress) {
Get-DhcpServerv4Scope -ComputerName $ComputerName | ForEach-Object {
Get-DhcpServerv4Lease -ComputerName $ComputerName -AllLeases -ScopeId ($_.ScopeID)
} | Where-Object 'ipaddress' -Match $IPAddress
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment