Skip to content

Commit

Permalink
Get-DHCPLease script added
Browse files Browse the repository at this point in the history
  • Loading branch information
adbertram committed May 26, 2016
1 parent fc7b067 commit 600ef08
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
92 changes: 92 additions & 0 deletions DHCP/Get-DhcpLease.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
describe 'Get-DhcpLease' {

mock 'Get-DhcpServerv4Scope' {
[pscustomobject]@{ 'ScopeId' = '10.10.10.0' }
}

mock 'Get-DhcpServerv4Lease' {
return $null
}

mock 'Get-DhcpServerInDc' {
@(
[pscustomobject]@{ 'DNSName' = 'SRV1' }
[pscustomobject]@{ 'DNSName' = 'SRV2' }
)
}

it 'queries all DHCP servers in domain if no DHCP server passed' {

@('SRV1', 'SRV2')
{
mock 'Test-Connection' {
$true
} -ParameterFilter { $Computername -eq $_ }

& "$PSScriptRoot\Get-DhcpLease.ps1"

Assert-MockCalled 'Test-Connection'
}
}

it 'only queries DHCP server passed' {

mock 'Test-Connection' {
$true
} -ParameterFilter { $Computername -eq 'SRV1' }

& "$PSScriptRoot\Get-DhcpLease.ps1" -DhcpServer 'SRV1'

Assert-MockCalled 'Test-Connection'

}

it 'limits leases to only those matching IP address' {

mock 'Get-DhcpServerv4Lease' {
return $null
} -ParamterFilter { $IpAddress -eq '1.1.1.1' }

& "$PSScriptRoot\Get-DhcpLease.ps1" -IpAddress '1.1.1.1' -DhcpServer 'SRV1'

Assert-MockCalled 'Get-DhcpServerv4Lease'
}

it 'limits leases to only those matching hostname' {

mock 'Get-DhcpServerv4Lease' {
return [pscustomobject]@{ 'HostName' = 'SRV2' }
} -ParamterFilter { $HostName -eq 'SRV2' }

& "$PSScriptRoot\Get-DhcpLease.ps1" -HostName 'SRV2' -DhcpServer 'SRV1'

Assert-MockCalled 'Get-DhcpServerv4Lease'

}

it 'limits leases to only those matching MAC address' {

mock 'Get-DhcpServerv4Lease' {
return $null
} -ParamterFilter { $ClientId -eq '00:00' }

& "$PSScriptRoot\Get-DhcpLease.ps1" -MacAddress '00:00' -DhcpServer 'SRV1'

Assert-MockCalled 'Get-DhcpServerv4Lease'

}

it 'returns a non-terminating error if a DHCP server to be queried is offline' {

mock 'Test-Connection' {
return $false
} -ParamterFilter {$ComputerName -eq 'SRV1'}

& "$PSScriptRoot\Get-DhcpLease.ps1" -DhcpServer 'SRV1' -ev myErr -ea SilentlyContinue

Assert-MockCalled 'Test-Connection'
$myErr | should not be nullorempty


}
}
85 changes: 85 additions & 0 deletions DHCP/Get-DhcpLease.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#Requires -Version 4 -Module DhcpServer

[CmdletBinding(DefaultParameterSetName = 'Default')]
param (
[Parameter()]
[ValidateNotNullOrEmpty()]
[string[]]$DhcpServer,

[Parameter(ParameterSetName = 'HostName')]
[ValidateNotNullOrEmpty()]
[string[]]$HostName,

[Parameter(ParameterSetName = 'MacAddress')]
[ValidateNotNullOrEmpty()]
[string[]]$MacAddress,

[Parameter(ParameterSetName = 'IpAddress')]
[ValidateNotNullOrEmpty()]
[ipaddress[]]$IpAddress
)

begin
{
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
Set-StrictMode -Version Latest
}

process
{
try
{
if (-not $PSBoundParameters.ContainsKey('DhcpServer'))
{
if ($DhcpServer = Get-DhcpServerInDC)
{
$DhcpServer = $DhcpServer.DnsName
}
}

@($DhcpServer).foreach({

try
{
$srv = $_
if (-not (Test-Connection -ComputerName $srv -Quiet -Count 1))
{
throw "The DHCP server [$($srv)] is not available."
}

$leaseParams = @{
'ComputerName' = $srv
}

if ($PSBoundParameters.ContainsKey('MacAddress')) {
$leaseParams.ClientId = $MacAddress
}

@(Get-DhcpServerv4Scope -ComputerName $srv).foreach({
if ($PSBoundParameters.ContainsKey('Ipaddress')) {
$leaseParams.IpAddress = $IpAddress
}
else
{
$leaseParams.ScopeId = $_.ScopeId
}

$leases = Get-DhcpServerv4Lease @leaseParams
if ($PSBoundParameters.ContainsKey('HostName')) {
@($leases).where({$_.Hostname -in $HostName})
}

})
}
catch
{
Write-Error -Message $_.Exception.Message
}

})
}
catch
{
Write-Error -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)"
}
}

0 comments on commit 600ef08

Please sign in to comment.