POLL: POWERSHELL VS. GUI - DO YOU WANT TO BE A DEVOP OR AN ADMIN?

Get DNS server IPs using PowerShell

In my last article I explained how to set the DNS IP addresses of remote computers and today I will show you how to get the DNS server IPs configured in network connections of local or remote computers using PowerShell.

A picture of Sitaram Pamarthi By Sitaram Pamarthi - Tue, September 11, 2012 - 5 comments

Sitaram Pamarthi is working as a Windows Engineer and his special fields of interest are PowerShell, Active Directory, Exchange, and virtualization.

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($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]
}

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 TIBPC1 – lists the DNS server IPs of network adapters on the remote computer TIBPC1

· .\Get-DNSServers.ps1 -ComputerName TIBPC1, TIBPC2, TIBPC3 – 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

Download the “Get DNS server IPs PowerShell” script: Get-DNSServers.ps1

Your question wasn't answered? Ask in the new 4sysops forum!

5 Comments - Leave a Reply

  1. Valentin says:

    So where is the script? I cannot see it anywhere?

  2. Valentine, that was my fault. I forgot the download link. You can find the link now at the end of the text. Thanks for the hint!

  3. techibee.com says:

    Thanks for pointing that ,Valentin . Please share your feedback on the script.

  4. Paul says:

    This script didn’t work for me. Is it supposed to be run against powershell v1 or v2?

  5. techibee says:

    Hi, what is the error message? This script works perfectly in powershell v2.

===Leave a Comment===