Created
January 18, 2022 08:40
-
-
Save JohanSelmosson/5400954b10bbda3520f372d4bba47d7e to your computer and use it in GitHub Desktop.
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
#Källa https://www.bvanleeuwen.nl/faq/?p=1182 | |
#Modifierat för att ta med info om failover status på scope-nivå | |
import-module DHCPServer | |
#Get all Authorized DCs from AD configuration | |
$DHCPs = Get-DhcpServerInDC | Where-Object {$_.DnsName -eq 'sa2netsrv01.prod.pk.local'} | |
$filename = "c:\nordlo\DHCPScopes_DNS_$(get-date -Uformat "%Y%m%d-%H%M%S").csv" | |
$Report = @() | |
$k = $null | |
write-host -foregroundcolor Green "`n`n`n`n`n`n`n`n`n" | |
foreach ($dhcp in $DHCPs) { | |
$k++ | |
Write-Progress -activity "Getting DHCP scopes:" -status "Percent Done: " -PercentComplete (($k / $DHCPs.Count) * 100) -CurrentOperation "Now processing $($dhcp.DNSName)" | |
$scopes = $null | |
$scopes = (Get-DhcpServerv4Scope -ComputerName $dhcp.DNSName -ErrorAction:SilentlyContinue) | |
If ($scopes -ne $null) { | |
#getting global DNS settings, in case scopes are configured to inherit these settings | |
$GlobalDNSList = $null | |
$GlobalDNSList = (Get-DhcpServerv4OptionValue -OptionId 6 -ComputerName $dhcp.DNSName -ErrorAction:SilentlyContinue).Value | |
$scopes | % { | |
$row = "" | select Hostname,ScopeID,SubnetMask,Name,State,StartRange,EndRange,LeaseDuration,Description,DNS1,DNS2,DNS3,GDNS1,GDNS2,GDNS3,DNSName,Router,FailoverName,FailoverState | |
$row.Hostname = $dhcp.DNSName | |
$row.ScopeID = $_.ScopeID | |
$row.SubnetMask = $_.SubnetMask | |
$row.Name = $_.Name | |
$row.State = $_.State | |
$row.StartRange = $_.StartRange | |
$row.EndRange = $_.EndRange | |
$row.LeaseDuration = $_.LeaseDuration | |
$row.Description = $_.Description | |
$ScopeDNSList = $null | |
$ScopeDNSList = (Get-DhcpServerv4OptionValue -OptionId 6 -ScopeID $_.ScopeId -ComputerName $dhcp.DNSName -ErrorAction:SilentlyContinue).Value | |
#write-host "Q: Use global scopes?: A: $(($ScopeDNSList -eq $null) -and ($GlobalDNSList -ne $null))" | |
If (($ScopeDNSList -eq $null) -and ($GlobalDNSList -ne $null)) { | |
$row.GDNS1 = $GlobalDNSList[0] | |
$row.GDNS2 = $GlobalDNSList[1] | |
$row.GDNS3 = $GlobalDNSList[2] | |
$row.DNS1 = $GlobalDNSList[0] | |
$row.DNS2 = $GlobalDNSList[1] | |
$row.DNS3 = $GlobalDNSList[2] | |
} | |
Else { | |
$row.DNS1 = $ScopeDNSList[0] | |
$row.DNS2 = $ScopeDNSList[1] | |
$row.DNS3 = $ScopeDNSList[2] | |
} | |
$row.DNSName = (Get-DhcpServerv4OptionValue -ComputerName $dhcp.DNSName -OptionId 15 -ScopeID $_.ScopeId).Value | |
$row.Router = (Get-DhcpServerv4OptionValue -ComputerName $dhcp.DNSName -OptionId 3 -ScopeID $_.ScopeId).Value | |
$failover = Get-DhcpServerv4Failover -ComputerName $dhcp.DNSName -ScopeId $_.ScopeId | |
$row.FailoverName = $failover.Name | |
$row.FailoverState = $failover.state | |
$Report += $row } | |
} | |
Else { | |
write-host -foregroundcolor Yellow """$($dhcp.DNSName)"" is either running Windows 2003, or is somehow not responding to querries. Adding to report as blank" | |
$row = "" | select Hostname,ScopeID,SubnetMask,Name,State,StartRange,EndRange,LeaseDuration,Description,DNS1,DNS2,DNS3,GDNS1,GDNS2,GDNS3,DNSName,Router,FailoverPartnerServer,FailoverName, | |
$row.Hostname = $dhcp.DNSName | |
$Report += $row | |
} | |
write-host -foregroundcolor Green "Done Processing ""$($dhcp.DNSName)""" | |
} | |
$Report | Export-csv -NoTypeInformation -Encoding Unicode -UseCulture $filename |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment