![]() |
I AM THE FUNNY!!! |
---|
Is there anything more annoying then trying to find which server a DHCP scope is located on? ... well yes, there are... but finding a scope can still be pretty damn annoying when all you are given is an IP address.
To help you out, here is a script that I prepared earlier.
<# .SYNOPSIS Searches DHCP servers to find the location of the scope .DESCRIPTION This script allows you to search your most heavily ustilised DHCP servers, before searching the rest of the DHCP server for a scope. It will also retunr servers that could not be searched as they are unreachable and any errors that occure when connecting to DHCP .PARAMETER <paramName> $Scope: Input from you to search, in the format XX.XX.XX.0 #> Import-Module DhcpServer # I am using a log to collect the errors I want, there will be better ways using the actual error, but... all or my meh $pathLog = "C:\Temp\Get-DHCPscoped.txt" # Function to write to the log Function LogWrite([string]$logstring){ Add-content $pathLog -value $logstring Start-Sleep -m 500 } # Function Get-DHCPscoped($DHCPServers, $count, $IP){ $Checking = 0 foreach($Server in $DHCPServers){ $Checking ++ [string]$breaking = $Server.dnsname # This step checks to make sure the server is online before attempting to connect if (test-Connection -ComputerName $Server.dnsname -Count 4 -Quiet){ Write-Host -NoNewline "Checking " Write-Host -NoNewline $Checking -ForegroundColor Green Write-Host -NoNewline " of " Write-Host -NoNewline "$count " -ForegroundColor Yellow write-host -NoNewline $Server.dnsname write-host get-dhcpserverv4scope -ComputerName $Server.dnsname | Select ScopeId, Name, State, @{Name="Server";Expression={ $Server.DnsName }} | Where{ $_.ScopeId.IPAddressToString -eq $IP } | Format-Table ScopeId, Name, State, Server -ErrorAction SilentlyContinue # This is checking to see if an error occurs when connecting to the DHCP server if($error.length -gt 0){ [string]$huehuehue = $error[0] if($huehuehue.EndsWith("$breaking.")){ Write-Warning "Issues connecting to DHCP service for $breaking" $towrite = $breaking + " - " + $huehuehue LogWrite $towrite } } }Else{ Write-Warning "$breaking currently unavailable" $towrite = $breaking + " - No ping response" LogWrite $towrite } } } $Scope = Read-Host -Prompt 'Input IP Address (XX.XX.XX.0)' $firstCheck =@() # Most domains might have some server which have a larger number of scopes (i.e. they host the scopes for serverless sites, or mulitple CBD site scopes) # This step allows you to input their names here to search for them first. $firstChecktoobj = "server1.iamthefunny.net","server2.iamthefunny.net","server3.iamthefunny.net" foreach($blah in $firstChecktoobj){ $myobjfirst = "" | Select DnsName $myobjfirst.DnsName = $blah $firstCheck += $myobjfirst } # Searching the main servers first Write-host Write-host "Searching three main DHCP servers" -BackgroundColor DarkCyan Write-host $Result = Get-DHCPscoped $firstCheck 3 $Scope if(!$Result){ Write-host Write-host "Scope was not found, searching all DHCP servers" -BackgroundColor Magenta Write-host # Gets all of the DHCP servers authorised on the domain $allthescopes = Get-DhcpServerInDC | sort-object dnsName # Counts how many there are so you know how many to go $TotalServers = $allthescopes.count $Result = Get-DHCPscoped $allthescopes $TotalServers $Scope } if(!$Result){ Write-host Write-host "Scope $Scope was not found" -BackgroundColor Red Write-host }Else{ Write-host Write-host "Scope details below" -ForegroundColor Green Write-host $Result } # Lists any servers that could not be searched and the reason if (Test-Path $pathLog){ $AllTheErrors = Get-Content -path $pathLog Write-host "Servers not checked:" -ForegroundColor Red Write-host "--------------------" -ForegroundColor Red ForEach($e in $AllTheErrors){Write-host $e -ForegroundColor Red} # Removes the log file as it was only to keep any errors Remove-Item $pathLog } Read-Host -Prompt "Press Enter to exit"