forked from adbertram/Random-PowerShell-Work
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 | ||
|
||
|
||
} | ||
} |
This file contains 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
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)" | ||
} | ||
} |