customer relationships

Get DNS server IP’s using PowerShell

On servers, we use static IP configuration and static DNS configuration. If you have multiple sites in your environment, this list of DNS server IPs will vary from one site’s server to another. Given these, we sometimes end up with incorrect DNS IP entries in some of the servers. The script I am going to discuss will help you query all your servers and display their DNS server settings.

This script uses the Win32_NetworkAdapterConfiguration WMI class to get the DNS server IPs of all network adapters on the computer. This list of network adapters contains both physical and virtual adapters. We are only interested in adapters that have an IP address (DHCP/Static) because adapters without an IP address won’t have any DNS server settings.

As shown in the code below, I am using the Get-WmiObject cmdlet to query the Win32_NetworkAdapterConfiguration WMI class. I pass a filter with IPEnabled=TRUE to ensure I am querying the network adapters with a configured IP address. The –ComputerName parameter takes the computer name against which this WMI query will be executed. I placed the entire WMI query inside a try block to catch the errors.try { $Networks = Get-WmiObject -Class Win32_NetworkAdapterConfiguration ` –Filter IPEnabled=TRUE ` -ComputerName $Computer ` -ErrorAction Stop } catch { Write-Verbose “Failed to Query $Computer. Error details: $_continue}

After the WMI query executes, we get a list of network adapters that have a configured IP address. The next step is to iterate through them and find out the DNS server IP addresses. You can see that in the code below. The DNSServerSearchOrder parameter of each network connection contains the DNS server IP addresses. After reading the DNS server IP address parameters, I take the output through several tests to ensure that it is not empty (because it is quite possible that we configured static IP addresses and left the DNS server fields empty, either intentionally or by mistake) and to determine whether just one DNS server IP is configured. If the list of DNS server IP addresses is empty, I set the $PrimaryDNSServer and $SecondaryDNSServer variables to “Not set.” If only one DNS server is configured, I set the $SecondaryDNSServer variable to “Not set” and assign the DNS server IP value to $PrimaryDNSServer. Both of these variables will be set to appropriate DNS server IP address values if both DNS server IP addresses are available in the network adapter.

foreach($Networkin$Networks) {

$DNSServers = $Network.DNSServerSearchOrder

$NetworkName = $Network.Description

If(!$DNSServers) {

$PrimaryDNSServer = “Notset”

$SecondaryDNSServer = “Notset”

} elseif($DNSServers.count -eq 1) {

$PrimaryDNSServer = $DNSServers[0]

$SecondaryDNSServer = “Notset” } else {

$PrimaryDNSServer = $DNSServers[0]

$SecondaryDNSServer = $DNSServers[1]

}

So far, we queried the required data and stored it in two variables ($PrimaryDNSServer and $SecondaryDNSServer). The next step is to create a custom PSObject to store the data and display it on the screen. The below code does exactly this:

$OutputObj = New-Object -Type PSObject

$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()

$OutputObj | Add-Member -MemberType NoteProperty -Name PrimaryDNSServers -Value $PrimaryDNSServer$OutputObj | Add-Member -MemberType NoteProperty -Name SecondaryDNSServers -Value

$SecondaryDNSServer

$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled

$OutputObj | Add-Member -MemberType NoteProperty -Name NetworkName -Value $NetworkName

$OutputObj

I query some more data, such as the description of the network adapter for easy identification of network connections that have DNS server IP addresses. I also populate another column in the output, called IsDHCPEnabled, to find out if the network adapter has DHCP given IP addresses or statically configured IP addresses.

Get DNS Server IP address with PowerShell

Get DNS Server IP address with PowerShell

Examples:

· .\Get-DNSServers.ps1 – lists the DNS server IPs of network adapters on the local computer

· .\Get-DNSServers.ps1 -ComputerName PC1 – lists the DNS server IPs of network adapters on the remote computer PC1

· .\Get-DNSServers.ps1 -ComputerName PC1, PC2, PC3 – lists the DNS server IPs of network adapters on multiple remote computers

· Get-Content c:\scripts\comps.txt | .\Get-DNSServers.ps1 – uses a text file called comps.txt to store the list of computers

POWERSHELL CODE:

<#
    .Synopsis 
        Get the DNS servers list of each IP enabled network connection
        
    .Description
        This script displays DNS servers list of each IP enabled network connection in local or remote computer.
 
    .Parameter ComputerName    
        Computer Name(s) from which you want to query the DNS server details. If this
		parameter is not used, the the script gets the DNS servers from local computer network adapaters.
        
    .Example 1
        Get-DNSServers.ps1 -ComputerName MYTESTPC21
        Get the DNS servers information from a remote computer MYTESTPC21.
		
    .Notes
        NAME:      Get-DNSServers.ps1
        

#>

[cmdletbinding()]
param (
	[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
	[string[]] $ComputerName = $env:computername
)

begin {}
process {
	foreach($Computer in $ComputerName) {
		Write-Verbose "Working on $Computer"
		if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
			
			try {
				$Networks = Get-WmiObject -Class Win32_NetworkAdapterConfiguration `
							-Filter IPEnabled=TRUE `
							-ComputerName $Computer `
							-ErrorAction Stop
			} catch {
				Write-Verbose "Failed to Query $Computer. Error details: $_"
				continue
			}
			foreach($Network in $Networks) {
				$DNSServers = $Network.DNSServerSearchOrder
				$NetworkName = $Network.Description
				If(!$DNSServers) {
					$PrimaryDNSServer = "Notset"
					$SecondaryDNSServer = "Notset"
				} elseif($DNSServers.count -eq 1) {
					$PrimaryDNSServer = $DNSServers[0]
					$SecondaryDNSServer = "Notset"
				} else {
					$PrimaryDNSServer = $DNSServers[0]
					$SecondaryDNSServer = $DNSServers[1]
				}
				If($network.DHCPEnabled) {
					$IsDHCPEnabled = $true
				}
				
				$OutputObj  = New-Object -Type PSObject
				$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
				$OutputObj | Add-Member -MemberType NoteProperty -Name PrimaryDNSServers -Value $PrimaryDNSServer
				$OutputObj | Add-Member -MemberType NoteProperty -Name SecondaryDNSServers -Value $SecondaryDNSServer
				$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
				$OutputObj | Add-Member -MemberType NoteProperty -Name NetworkName -Value $NetworkName
				$OutputObj
				
			}
		} else {
			Write-Verbose "$Computer not reachable"
		}
	}
}

end {}


Got a project that needs expert IT support?

From Linux and Microsoft Server to VMware, networking, and more, our team at CR Tech is here to help.

Get personalized support today and ensure your systems are running at peak performance or make sure that your project turns out to be a successful one!

CONTACT US NOW
best vpn deal