PowerShell Script – Check the Windows Server DNS Settings

I recently have a task to review the DNS settings for more than 100 Windows servers in the same domain. I drafted a simple PowerShell script to perform the Windows DNS checking and export all the server DNS configuration into a CSV file.

#This is a PowerShell Script to check the DNS settings for the servers from a given CSV file
#Example:  CheckDNSSetting.ps1 -i serverlist.csv

#Reference:  https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-networkadapterconfiguration

param
(
   [alias("i")]
   [string]$inputfile = $(read-host -Prompt "Enter the full path to the list of the CSV input file")
   
)


#Initialized the output file
$TimeStamp1 = $(((get-date).ToUniversalTime()).ToString("yyyyMMddTHHmmssZ"))
$output = "C:\temp\ExportedDNSSetting$TimeStamp1.csv"

Write-Host "--->Generating the new output file now...... $output " 
New-Item -Path $output -ItemType File > $null
Add-Content -Path $output  -Value '"Hostname","Adapter Index","IP Address","DNSServerSetting"'


#Generate the DNS settings output
Import-Csv $inputfile | foreach {

$AdapaterSettings = Get-wmiobject -ClassName Win32_NetworkAdapterConfiguration -ComputerName $_.hostname -ErrorAction SilentlyContinue

if($AdapaterSettings -eq $null)  {

        Write-Host "--->Collect the DNS Configuration Failed for server -" $_.hostname  -ForegroundColor Red

        $NewLine = "{0},{1},{2},{3}" `
            -f  $_.hostname,`
                $null,`
                $null,`
                "This server cannot be connected!!!"

        $NewLine | Add-Content -Path $output
        
        }else  {
                
                Write-Host "--->Collect the DNS Configuration successfully for server -" $_.hostname -ForegroundColor Green
                
                Foreach ($ThisAdapaterSetting in $AdapaterSettings) {

                    $NewLine = "{0},{1},{2},{3}" `
                        -f  $_.hostname,`
                            $ThisAdapaterSetting.Index,`
                            [string]$ThisAdapaterSetting.IPAddress,`
                            [string]$ThisAdapaterSetting.DNSServerSearchOrder

                    $NewLine | Add-Content -Path $output

                    }
                }

}

Write-Host "--->Data Collection is completed, the result is at $output"

The example is as below:

The example result CSV file is as below:

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s