PowerShell Script for DHCP settings, leases and reservations inventory


Another day, another PowerShell script.. this time to get DHCP settings, leases and reservations from all DHCP Servers in current domain. Pre-requisite is all DHCP Servers should be Windows 2012 based and WinRM should be enabled on all of them.

import-module DHCPServer

#Get all Authorized DCs from AD configuration
$DHCPs = Get-DhcpServerInDC
$filename = "$Env:userprofile\Desktop\DHCPScopes_DNS_$(get-date -Uformat "%Y%m%d-%H%M%S").csv"
$Report = @()

$Reservations = @()
$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)	     
	#$Res = $scopes | ForEach {Get-DHCPServerv4Lease -ComputerName $dhcp.DNSName -ScopeID $_.ScopeID | where {$_.AddressState -like '*Reservation'}} | Select-Object ScopeId,IPAddress,HostName,Description,ClientID,AddressState
	$Res = $scopes | ForEach {Get-DHCPServerv4Lease -ComputerName $dhcp.DNSName -ScopeID $_.ScopeID} | Select-Object ScopeId,IPAddress,HostName,Description,ClientID,AddressState

	write-host -foregroundcolor Green "Processing reservations on $($dhcp.DNSName)"
	ForEach($Temp in $Res ){
	$Reservation = New-Object -TypeName PSObject -Property @{
        	ServerName = $dhcp.DNSName
		ScopeID = $Temp.ScopeId
		IPAddress = $Temp.IPAddress
		HostName = $Temp.HostName
		Description = $Temp.Description
		ClientID = $Temp.ClientID
		AddressState = $Temp.AddressState        
	} | select-object ServerName, ScopeID, IPAddress, HostName, Description, ClientID, AddressState
	$Reservations += $Reservation
    } 	
	
    If ($scopes -ne $null) {
        $GlobalDNSList = $null        
        #getting global DNS settings, in case scopes are configured to inherit these settings
        $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,DNS4,DNS5,GDNS1,GDNS2,GDNS3,GDNS4,GDNS5,Router
            
	$router = @()		
	Try { $router = (Get-DhcpServerv4OptionValue -ComputerName $dhcp.DNSName -OptionId 3 -ScopeID $_.ScopeId -ErrorAction:SilentlyContinue).Value}
	Catch{ $router = ("")}
            $row.Router = $router[0]
            $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            

If (($ScopeDNSList -eq $null) -and ($GlobalDNSList -ne $null)) {
                $row.GDNS1 = $GlobalDNSList[0]
                $row.GDNS2 = $GlobalDNSList[1]
                $row.GDNS3 = $GlobalDNSList[2]
                $row.GDNS4 = $GlobalDNSList[3]
                $row.GDNS5 = $GlobalDNSList[4]              
                }
            ElseIf (($ScopeDNSList -ne $null) -and ($GlobalDNSList -ne $null)) {
                $row.GDNS1 = $GlobalDNSList[0]
                $row.GDNS2 = $GlobalDNSList[1]
                $row.GDNS3 = $GlobalDNSList[2]
                $row.GDNS4 = $GlobalDNSList[3]
                $row.GDNS5 = $GlobalDNSList[4]                				
				$row.DNS1 = $ScopeDNSList[0]
                $row.DNS2 = $ScopeDNSList[1]
                $row.DNS3 = $ScopeDNSList[2]
                $row.DNS4 = $ScopeDNSList[3]
                $row.DNS5 = $ScopeDNSList[4]                
                }
			Else {
				$row.DNS1 = $ScopeDNSList[0]
                $row.DNS2 = $ScopeDNSList[1]
                $row.DNS3 = $ScopeDNSList[2]
                $row.DNS4 = $ScopeDNSList[3]
                $row.DNS5 = $ScopeDNSList[4]                
			}	
            $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,DNS4,DNS5,GDNS1,GDNS2,GDNS3,GDNS4,GDNS5,Router
        $row.Hostname = $dhcp.DNSName
        $Report += $row
        }
    write-host -foregroundcolor Green "Done Processing ""$($dhcp.DNSName)"""
    }
$Report  | Export-csv -NoTypeInformation -UseCulture $filename
$Reservations | Export-Csv "$Env:userprofile\Desktop\DHCPLeasesReservations_$(get-date -Uformat "%Y%m%d-%H%M%S").csv" -NoTypeInformation
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s