Last active
April 5, 2019 16:41
-
-
Save alan-finn/fb38da2f8ec6e3642f2b1fea0fca710e to your computer and use it in GitHub Desktop.
Menu wrapper for a few DHCP cmdlets
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
function Import-RemoteDhcpServerModule { | |
param ( | |
$server | |
) | |
$Error.Clear() | |
Write-Output "`nCreating remote session to $server..." | |
try { | |
$Script:Session = New-PSSession -ComputerName $server | |
Invoke-Command -Command { Import-Module dhcpserver } -Session $Script:Session | |
Import-PSSession -Session $Session -Module dhcpserver -AllowClobber | |
} | |
catch { | |
Write-Host "Error establishing session to $server" -ForegroundColor Yellow | |
Write-Host $Error[0].Exception.Message -ForegroundColor Red -BackgroundColor Black | |
break | |
} | |
} | |
function Select-DhcpDhcpServer { | |
Clear-Host | |
Write-Output "`nEnumerating DHCP Servers..." | |
$DhcpServers = Get-DhcpServerInDC | Sort-Object -Property DnsName | Select-Object -ExpandProperty DnsName | |
$htDhcpServers = @{} | |
$DhcpServerList = "`n Available DHCP Servers" | |
$DhcpServerList += "`n ----------------------`n" | |
$i = 0 | |
foreach ($DhcpServer in $DhcpServers) { | |
$htDhcpServers.Add($i,$DhcpServer) | |
$i++ | |
} | |
for ($x=0;$x -lt $DhcpServers.Count;$x++) { | |
$DhcpServerList += " $x - $($htDhcpServers.$x)`n" | |
} | |
$DhcpServerList += " $($x+1) - Quit`n" | |
$DhcpServerList | |
do { | |
if ($null -ne $test) { | |
Write-Output "`nIncorrect selection, please try again." | |
} | |
$DHCPServerTarget = Read-Host -Prompt "Select the target DHCP server" | |
$Script:SelectedDhcpServer = $htDhcpServers.[int]$DHCPServerTarget | |
$test = $false | |
} while ($DHCPServerTarget -notmatch "^([0-9]|[1-2][0-8])$") | |
if ($DHCPServerTarget -eq $x+1) { | |
$Script:ExitScript = $true | |
return | |
} | |
else { | |
$Script:ExitScript = $false | |
} | |
Import-RemoteDhcpServerModule -server $Script:SelectedDhcpServer | |
} | |
function Show-DhcpMainMenu { | |
Clear-Host | |
$MainMenuBadChoice = $false | |
do { | |
$MainMenu = @" | |
DHCP Utility - Main Menu | |
------------------------ | |
1 - View details for a specific scope | |
2 - View reservations for a specific scope | |
3 - View statistics for a specific scope | |
4 - View leases for a specific scope | |
5 - View scope summary for current server | |
6 - Add reservation | |
7 - Convert existing lease to reservation | |
8 - Delete reservation | |
9 - Delete lease | |
10 - Quit | |
(Selected server: ${SelectedDhcpServer}) | |
Selection | |
"@ | |
if ($MainMenuBadChoice -eq $true) { | |
Clear-Host | |
Write-Host "`nInvalid selection: $MainMenuSelection`n" -ForegroundColor Yellow | |
} | |
$MainMenuSelection = Read-Host -Prompt $MainMenu | |
$MainMenuBadChoice = $true | |
} while ($MainMenuSelection -notmatch "^([1-9]|10)$") | |
switch ($MainMenuSelection) { | |
'1' { | |
$Error.Clear() | |
$TargetScopeId = Read-Host -Prompt "`nEnter the IP address (network ID) for the scope" | |
Get-DhcpScopeDetails -ScopeId $TargetScopeId | |
if ($Error.Count -gt 0) { Show-DhcpMainMenu } | |
} | |
'2' { | |
$Error.Clear() | |
$TargetScopeId = Read-Host -Prompt "`nEnter the IP address (network ID) for the scope" | |
Get-DhcpScopeReservation -ScopeId $TargetScopeId | |
if ($Error.Count -gt 0) { Show-DhcpMainMenu } | |
} | |
'3' { | |
$Error.Clear() | |
$TargetScopeId = Read-Host -Prompt "`nEnter the IP address (network ID) for the scope" | |
Get-DhcpScopeStatistics -ScopeId $TargetScopeId | |
if ($Error.Count -gt 0) { Show-DhcpMainMenu } | |
} | |
'4' { | |
$Error.Clear() | |
$TargetScopeId = Read-Host -Prompt "`nEnter the IP address (network ID) for the scope" | |
Get-DhcpScopeLeases -ScopeId $TargetScopeId | |
if ($Error.Count -gt 0) { Show-DhcpMainMenu } | |
} | |
'5' { | |
$Error.Clear() | |
Get-DhcpScopeSummary | |
if ($Error.Count -gt 0) { Show-DhcpMainMenu } | |
} | |
'6' { | |
$Error.Clear() | |
Add-DhcpReservation | |
if ($Error.Count -gt 0) { Show-DhcpMainMenu } | |
} | |
'7' { | |
$Error.Clear() | |
$TargetIpAddress = Read-Host -Prompt "`nEnter the IP address of the lease to be converted to a reservation" | |
Set-DhcpLeaseToReservation -IpAddress $TargetIpAddress | |
if ($Error.Count -gt 0) { Show-DhcpMainMenu } | |
} | |
'8' { | |
$Error.Clear() | |
$TargetIpAddress = Read-Host -Prompt "`nEnter the IP address of the reservation to be deleted" | |
Remove-DhcpReservation -IpAddress $TargetIpAddress | |
if ($Error.Count -gt 0) { Show-DhcpMainMenu } | |
} | |
'9' { | |
$Error.Clear() | |
$TargetIpAddress = Read-Host -Prompt "`nEnter the IP address of the lease to be deleted" | |
Remove-DhcpLease -IpAddress $TargetIpAddress | |
if ($Error.Count -gt 0) { Show-DhcpMainMenu } | |
} | |
'10' { | |
Remove-PSSession $Script:Session | |
return | |
} | |
default { | |
Write-Host "Incorrect value selected." -ForegroundColor Yellow | |
} | |
} | |
} | |
function Get-DhcpScopeDetails { | |
param ( | |
[Parameter(Mandatory = $true)] | |
[IPAddress] $ScopeId | |
) | |
try { | |
Get-DhcpServerv4Scope -ScopeId $ScopeId -ErrorAction Stop | Select-Object -Property IPAddress,ScopeId,AddressState,ClientId,Description,Name,Type | |
Select-MenuReturnOption | |
} | |
catch { | |
Write-Host "`t$_.Exception" -ForegroundColor Red -BackgroundColor Black | |
Read-Host -Prompt "Press [ENTER] to continue" | |
} | |
} | |
function Get-DhcpScopeReservation { | |
param ( | |
[Parameter(Mandatory = $true)] | |
[IPAddress] $ScopeId | |
) | |
try { | |
Get-DhcpServerv4Reservation -ScopeId $ScopeId -ErrorAction Stop | Select-Object -Property IPAddress,ScopeId,AddressState,ClientId,Description | |
Select-MenuReturnOption | |
} | |
catch { | |
Write-Host "`t$_.Exception" -ForegroundColor Red -BackgroundColor Black | |
Read-Host -Prompt "Press [ENTER] to continue" | |
} | |
} | |
function Get-DhcpScopeStatistics { | |
param ( | |
[Parameter(Mandatory = $true)] | |
[IPAddress] $ScopeId | |
) | |
try { | |
Get-DhcpServerv4ScopeStatistics -ScopeId $ScopeId -ErrorAction Stop | Select-Object -Property ScopeId,Free,InUse,Reserved,Pending,AddressesFree,PercentageInUse,ReservedAddressSuperscopeName | |
Select-MenuReturnOption | |
} | |
catch { | |
Write-Host "`t$_.Exception" -ForegroundColor Red -BackgroundColor Black | |
Read-Host -Prompt "Press [ENTER] to continue" | |
} | |
} | |
function Get-DhcpScopeLeases { | |
param ( | |
[Parameter(Mandatory = $true)] | |
[IPAddress] $ScopeId | |
) | |
try { | |
Get-DhcpServerv4Lease -ScopeId $ScopeId -ErrorAction Stop | Select-Object -Property HostName,IPAddress,AddressState,ClientId,ClientType,Description,DnsRegistration,DnsRR | |
Select-MenuReturnOption | |
} | |
catch { | |
Write-Host "`t$_.Exception" -ForegroundColor Red -BackgroundColor Black | |
Read-Host -Prompt "Press [ENTER] to continue" | |
} | |
} | |
function Get-DhcpScopeSummary { | |
try { | |
Get-DhcpServerv4Scope -ErrorAction Stop | Select-Object -Property ScopeId,Name,SubnetMask,StartRange,EndRange,LeaseDuration,State | |
Select-MenuReturnOption | |
} | |
catch { | |
Write-Host "`t$_.Exception" -ForegroundColor Red -BackgroundColor Black | |
Read-Host -Prompt "Press [ENTER] to continue" | |
} | |
} | |
function Add-DhcpReservation { | |
$IncorrectFormat = $false | |
do { | |
if ($IncorrectFormat -eq $true) { | |
Write-Host "`nMac address incorrectly formatted: $ReservationMac`n" -ForegroundColor Yellow | |
} | |
$ReservationMac = Read-Host -Prompt "`nEnter the MAC address in the format XX-XX-XX-XX-XX-XX" | |
$IncorrectFormat = $true | |
} while ($ReservationMac -notmatch "^([0-9a-fA-F][0-9a-fA-F]-){5}([0-9a-fA-F][0-9a-fA-F])$") | |
$ReservationScopeId = Read-Host -Prompt "`nEnter the IP address (network ID) for the scope" | |
# Check if Mac already in use. | |
try { | |
$ExistingReservation = Get-DhcpServerv4Lease -ScopeId $ReservationScopeId -ClientId $ReservationMac -ErrorAction Stop | |
if ($null -ne $ExistingReservation.ClientId) { | |
Write-Host "`nThe Mac address: $($ExistingReservation.ClientId) already exists. Please verify the Mac address and try again." -ForegroundColor Yellow | |
Add-DhcpReservation | |
} | |
} | |
catch { | |
# Mac address does not exist which throws an error on the Get-DhcpServerv4Lease cmdlet. | |
} | |
try { | |
do { | |
Clear-Variable IpCheck | |
$ReservationIPAddress = Read-Host -Prompt "`nEnter the IP address for the reservation" | |
$IpCheck = Get-DhcpServerv4FreeIPAddress -ScopeId $ReservationScopeId -StartAddress $ReservationIPAddress -EndAddress $ReservationIPAddress | |
if ($null -eq $IpCheck) { | |
Write-Host "$ReservationIPAddress is already in use. Select a different IP address." -ForegroundColor Yellow | |
} | |
} while ($null -eq $IpCheck) | |
} | |
catch { | |
Write-Host "`t$_.Exception" -ForegroundColor Red -BackgroundColor Black | |
Read-Host -Prompt "Press [ENTER] to continue" | |
} | |
$ReservationDescription = Read-Host -Prompt "`nEnter a description for the reservation (leave blank and press [ENTER] for none)" | |
try { | |
if ($ReservationDescription.Length -gt 0) { | |
Add-DhcpServerv4Reservation -ScopeId $ReservationScopeId -IPAddress $ReservationIPAddress -Description $ReservationDescription | |
Select-MenuReturnOption | |
} | |
else { | |
Add-DhcpServerv4Reservation -ScopeId $ReservationScopeId -IPAddress $ReservationIPAddress | |
Select-MenuReturnOption | |
} | |
} | |
catch { | |
Write-Host "`t$_.Exception" -ForegroundColor Red -BackgroundColor Black | |
Read-Host -Prompt "Press [ENTER] to continue" | |
} | |
} | |
function Set-DhcpLeaseToReservation { | |
param ( | |
[Parameter(Mandatory = $true)] | |
[IPAddress] $IpAddress | |
) | |
try { | |
Get-DhcpServerv4Lease -IPAddress $IpAddress -ErrorAction Stop | Add-DhcpServerv4Reservation | |
Select-MenuReturnOption | |
} | |
catch { | |
Write-Host "`t$_.Exception" -ForegroundColor Red -BackgroundColor Black | |
Write-Host "`n`tCheck to see if reservation already exists." -ForegroundColor Red -BackgroundColor Black | |
Read-Host -Prompt "Press [ENTER] to continue" | |
} | |
} | |
function Remove-DhcpReservation { | |
param ( | |
[Parameter(Mandatory = $true)] | |
[IPAddress] $IpAddress | |
) | |
try { | |
Remove-DhcpServerv4Reservation -IPAddress $IpAddress -ErrorAction Stop | |
Select-MenuReturnOption | |
} | |
catch { | |
Write-Host "`t$_.Exception" -ForegroundColor Red -BackgroundColor Black | |
Read-Host -Prompt "Press [ENTER] to continue" | |
} | |
} | |
function Remove-DhcpLease { | |
param ( | |
[Parameter(Mandatory = $true)] | |
[IPAddress] $IpAddress | |
) | |
try { | |
Remove-DhcpServerv4Lease -IPAddress $IpAddress -ErrorAction Stop | |
Select-MenuReturnOption | |
} | |
catch { | |
Write-Host "`t$_.Exception" -ForegroundColor Red -BackgroundColor Black | |
Read-Host -Prompt "Press [ENTER] to continue" | |
} | |
} | |
function Select-MenuReturnOption { | |
$MenuReturnBadChoice = $false | |
do { | |
$MainMenu = @" | |
1 - Return to main menu | |
2 - Quit | |
Selection | |
"@ | |
if ($MenuReturnBadChoice -eq $true) { | |
Clear-Host | |
Write-Host "`nInvalid selection: $MenuReturnSelection`n" -ForegroundColor Yellow | |
} | |
$MenuReturnSelection = Read-Host -Prompt $MainMenu | |
$MenuReturnBadChoice = $true | |
} while ($MenuReturnSelection -notmatch "^[1-2]$") | |
switch ($MenuReturnSelection) { | |
'1' { | |
$Error.Clear() | |
Show-DhcpMainMenu | |
if ($Error.Count -gt 0) { Select-MenuReturnOption } | |
} | |
'2' { | |
$Error.Clear() | |
Remove-PSSession $Script:Session | |
return | |
} | |
default { | |
Write-Host "Incorrect value selected." -ForegroundColor Yellow | |
} | |
} | |
} | |
Select-DhcpDhcpServer | |
if (-not $Script:ExitScript) { | |
Show-DhcpMainMenu | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment