Freigeben über


DNS Forwarders Fours Ways

I'm lucky enough to occasionally eat in nice restaurants. A lot of the time, the menu makes for interesting (and pretentious) reading. One may see things along the lines of:

  • Deconstructed green forest gateaux
  • Condensed cauliflower cheese cake
  • Themes of cucumber on a medley of mangoes
  • Wafts of autumn mushroom mist
  • Everything-tastes-like-chicken three ways

 

And, so taking inspiration from one of my favourite, occasional pass times, I serve you "DNS Forwarders Four Ways". Enjoy.

 

#1 - DNSCMD.exe

 
###################
#DNSCMD.exe method
###################

$Servers = "HALODC01","HALODC02" 

$TotalForwarders = @()

foreach ($Server in $Servers) {
 
    $Hostnames = @()
    $Forwarders = @()

    $DnsInfo = dnscmd.exe $server /info 

    if ($DnsInfo -notcontains "Info query failed") {
    
        $ForwardersLine = ($DnsInfo | Select-String -CaseSensitive "Forwarders").LineNumber

        $AddressCount = $ForwardersLine + 3

        $AddressCount = $DnsInfo[$AddressCount] -Split "="

        $AddressCount = $AddressCount[1].Trim()

        $ForwardersLine = $ForwardersLine + 4

        for ($i = 0; $i -lt $AddressCount; $i++) {

            $ForwardersSplit = $DnsInfo[$ForwardersLine + $i] -split "="

            $ForwardersIp = $ForwardersSplit[-1] 

            $Hostname = (Get-ADDomainController -Filter {ipv4address -eq $ForwardersIp}).Name

            $Forwarders += $ForwardersIp

            $Hostnames += $Hostname

        }

        $Properties = [pscustomobject]@{

            Server = $Server
            Hostnames = $Hostnames
            Forwarders = $Forwarders

        }

        $TotalForwarders += $Properties

    }
    else {

        Write-Output "Info query failed for $Server"

    }

}

 

#2 - Get-WMiObject

 
######################
#GET-WMIOBJECT method
######################

$Servers = "HALODC01","HALODC02" 

$TotalForwarders = @()

foreach ($Server in $Servers) {
 
    $Hostnames = @()
    $Forwarders = @()

    $DnsInfo = (Get-WmiObject -Class microsoftdns_server -Namespace root/microsoftdns -ComputerName $Server).Forwarders

    if ($DnsInfo) {
    
        foreach ($ForwarderIp in $DnsInfo) {


            $Hostname = (Get-ADDomainController -Filter {ipv4address -eq $ForwarderIp}).Name

            $Forwarders += $ForwarderIp

            $Hostnames += $Hostname

        }

        $Properties = [pscustomobject]@{

            Server = $Server
            Hostnames = $Hostnames
            Forwarders = $Forwarders

        }

        $TotalForwarders += $Properties

    }
    else {

        Write-Output "Info query failed for $Server"

    }

}

$TotalForwarders 

 

#3 - Get-CimInstance

 
########################
#GET-CIMINSTANCE method
########################

$Servers = "HALODC01","HALODC02" 

$TotalForwarders = @()

foreach ($Server in $Servers) {
 
    $Hostnames = @()
    $Forwarders = @()

    $DnsInfo = (Get-CimInstance -ClassName microsoftdns_server -Namespace root/microsoftdns -ComputerName $Server).Forwarders

    if ($DnsInfo) {
    
        foreach ($ForwarderIp in $DnsInfo) {


            $Hostname = (Get-ADDomainController -Filter {ipv4address -eq $ForwarderIp}).Name

            $Forwarders += $ForwarderIp

            $Hostnames += $Hostname

        }

        $Properties = [pscustomobject]@{

            Server = $Server
            Hostnames = $Hostnames
            Forwarders = $Forwarders

        }

        $TotalForwarders += $Properties

    }
    else {

        Write-Output "Info query failed for $Server"

    }

}

$TotalForwarders

 

#4 - Get-DnsServerForwarder

 
###############################
#GET-DNSSERVERFORWARDER method
###############################

$Servers = "HALODC01","HALODC02" 

$TotalForwarders = @()

foreach ($Server in $Servers) {
 
    $Hostnames = @()
    $Forwarders = @()

    $DnsInfo = (Get-DnsServerForwarder -ComputerName $Server).IPAddress

    if ($DnsInfo) {
    
        foreach ($ForwarderIp in $DnsInfo) {


            $Hostname = (Get-ADDomainController -Filter {ipv4address -eq $ForwarderIp}).Name

            $Forwarders += $ForwarderIp

            $Hostnames += $Hostname

        }

        $Properties = [pscustomobject]@{

            Server = $Server
            Hostnames = $Hostnames
            Forwarders = $Forwarders

        }

        $TotalForwarders += $Properties

    }
    else {

        Write-Output "Info query failed for $Server"

    }

}

$TotalForwarders