Report DHCP Scope Settings using Powershell

Source: http://www.rivnet.ro/2013/06/report-dhcp-scope-settings-using-powershell.html

 

A script to export information from all authorized DHCP servers in the Active directory. It will export the following information to a csv file:

DHCPServer name, Scope Name, Subnet defined, Start and End Ranges, Lease Times, Description, DNS Server, Gateway

 

import-module DHCPServer
#Get all Authorized DCs from AD configuration
$DHCPs = Get-DhcpServerInDC
$filename = "d:\backup\dhcp\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,Router
            $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]
                }
            $router = (Get-DhcpServerv4OptionValue -ComputerName $dhcp.DNSName -OptionId 3 -ScopeID $_.ScopeId).Value
            $row.Router = $router[0]
            $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,Router
        $row.Hostname = $dhcp.DNSName
        $Report += $row
        }
    write-host -foregroundcolor Green "Done Processing ""$($dhcp.DNSName)"""
    }

$Report  | Export-csv -NoTypeInformation -UseCulture $filename

 

Leave a Reply

Your email address will not be published. Required fields are marked *