{"id":3173,"date":"2021-09-13T17:38:34","date_gmt":"2021-09-13T22:38:34","guid":{"rendered":"https:\/\/microsoftgeek.com\/?p=3173"},"modified":"2021-09-13T17:54:38","modified_gmt":"2021-09-13T22:54:38","slug":"get-dns-server-ips-using-powershell","status":"publish","type":"post","link":"https:\/\/microsoftgeek.com\/?p=3173","title":{"rendered":"Get DNS server IP&#8217;s using PowerShell"},"content":{"rendered":"\n<p>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\u2019s 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.<\/p>\n\n\n\n<p>This script uses the\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa394217%28v=vs.85%29.aspx\" target=\"_blank\" rel=\"noreferrer noopener\">Win32_NetworkAdapterConfiguration<\/a>\u00a0WMI 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\u2019t have any DNS server settings.<\/p>\n\n\n\n<p>As shown in the code below, I am using the&nbsp;<strong>Get-WmiObject<\/strong>&nbsp;cmdlet to query the&nbsp;<strong>Win32_NetworkAdapterConfiguration<\/strong>&nbsp;WMI class. I pass a filter with&nbsp;<strong>IPEnabled=TRUE<\/strong>&nbsp;to ensure I am querying the network adapters with a configured IP address. The&nbsp;<strong>\u2013ComputerName<\/strong>&nbsp;parameter takes the computer name against which this WMI query will be executed. I placed the entire WMI query inside a&nbsp;<strong>try&nbsp;<\/strong>block to catch the errors.<strong>try<\/strong> { <strong>$Networks<\/strong> = Get-WmiObject -Class Win32_NetworkAdapterConfiguration ` &#8211;<strong>Filter<\/strong> IPEnabled=TRUE ` -ComputerName <strong>$Computer<\/strong> ` -ErrorAction Stop } <strong>catch<\/strong> { Write-Verbose &#8220;Failed to Query <strong>$Computer<\/strong>. Error details: <strong>$_<\/strong>&#8221; <strong>continue<\/strong>}<\/p>\n\n\n\n<p>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\u00a0<strong>DNSServerSearchOrder\u00a0<\/strong>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\u00a0<strong>$PrimaryDNSServer<\/strong>\u00a0and\u00a0<strong>$SecondaryDNSServer\u00a0<\/strong>variables to \u201cNot set.\u201d If only one DNS server is configured, I set the\u00a0<strong>$SecondaryDNSServer\u00a0<\/strong>variable to \u201cNot set\u201d and assign the DNS server IP value to\u00a0<strong>$PrimaryDNSServer<\/strong>. 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.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#7020c5\"><strong>foreach<\/strong>(<strong>$Networkin$Networks<\/strong>) { <\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#7020c5\"><strong>$DNSServers<\/strong> = <strong>$Network<\/strong>.DNSServerSearchOrder <\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#7020c5\"><strong>$NetworkName<\/strong> = <strong>$Network<\/strong>.Description <\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#7020c5\"><strong>If<\/strong>(!<strong>$DNSServers<\/strong>) { <\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#7020c5\"><strong>$PrimaryDNSServer<\/strong> = &#8220;Notset&#8221; <\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#7020c5\"><strong>$SecondaryDNSServer<\/strong> = &#8220;Notset&#8221; <\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#7020c5\">} <strong>elseif<\/strong>(<strong>$DNSServers<\/strong>.count -eq 1) { <\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#7020c5\"><strong>$PrimaryDNSServer<\/strong> = <strong>$DNSServers<\/strong>[0] <\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#7020c5\"><strong>$SecondaryDNSServer<\/strong> = &#8220;Notset&#8221; } <strong>else<\/strong> { <\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#7020c5\"><strong>$PrimaryDNSServer<\/strong> = <strong>$DNSServers<\/strong>[0] <\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#7020c5\"><strong>$SecondaryDNSServer<\/strong> = <strong>$DNSServers<\/strong>[1]<\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#7020c5\">}<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>So far, we queried the required data and stored it in two variables (<strong>$PrimaryDNSServer and $SecondaryDNSServer<\/strong>). 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:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#700a9b\"><strong>$OutputObj<\/strong> = New-Object -Type PSObject<\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#700a9b\"><strong>$OutputObj<\/strong> | Add-Member -MemberType NoteProperty -Name ComputerName -Value <strong>$Computer<\/strong>.ToUpper()<\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#700a9b\"><strong>$OutputObj<\/strong> | Add-Member -MemberType NoteProperty -Name PrimaryDNSServers -Value <strong>$PrimaryDNSServer$OutputObj<\/strong> | Add-Member -MemberType NoteProperty -Name SecondaryDNSServers -Value <\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#700a9b\"><strong>$SecondaryDNSServer<\/strong><\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#700a9b\"><strong>$OutputObj<\/strong> | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value <strong>$IsDHCPEnabled<\/strong><\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#700a9b\"><strong>$OutputObj<\/strong> | Add-Member -MemberType NoteProperty -Name NetworkName -Value <strong>$NetworkName<\/strong><\/p>\n\n\n\n<p class=\"has-text-color\" style=\"color:#700a9b\"><strong>$OutputObj<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>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&nbsp;<strong>IsDHCPEnabled<\/strong>,<strong>&nbsp;<\/strong>to find out if the network adapter has DHCP given IP addresses or statically configured IP addresses.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/4sysops.com\/wp-content\/uploads\/2012\/09\/Get-DNS-Server-IP-address-with-PowerShell_thumb.png\" alt=\"Get DNS Server IP address with PowerShell\" title=\"Get DNS Server IP address with PowerShell\"\/><\/figure>\n\n\n\n<p><em>Get DNS Server IP address with PowerShell<\/em><a><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examples:<\/h2>\n\n\n\n<p>\u00b7&nbsp;<strong>.\\Get-DNSServers.ps1<\/strong>&nbsp;\u2013 lists the DNS server IPs of network adapters on the local computer<\/p>\n\n\n\n<p>\u00b7\u00a0<strong>.\\Get-DNSServers.ps1 -ComputerName PC1\u00a0<\/strong>\u2013 lists the DNS server IPs of network adapters on the remote computer PC1<\/p>\n\n\n\n<p>\u00b7\u00a0<strong>.\\Get-DNSServers.ps1 -ComputerName PC1, PC2, PC3<\/strong>\u00a0\u2013 lists the DNS server IPs of network adapters on multiple remote computers<\/p>\n\n\n\n<p>\u00b7\u00a0<strong>Get-Content c:\\scripts\\comps.txt | .\\Get-DNSServers.ps1<\/strong>\u00a0\u2013 uses a text file called comps.txt to store the list of computers<\/p>\n\n\n\n<p><strong>POWERSHELL CODE:<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;#\r\n    .Synopsis \r\n        Get the DNS servers list of each IP enabled network connection\r\n        \r\n    .Description\r\n        This script displays DNS servers list of each IP enabled network connection in local or remote computer.\r\n \r\n    .Parameter ComputerName    \r\n        Computer Name(s) from which you want to query the DNS server details. If this\r\n\t\tparameter is not used, the the script gets the DNS servers from local computer network adapaters.\r\n        \r\n    .Example 1\r\n        Get-DNSServers.ps1 -ComputerName MYTESTPC21\r\n        Get the DNS servers information from a remote computer MYTESTPC21.\r\n\t\t\r\n    .Notes\r\n        NAME:      Get-DNSServers.ps1\r\n        \r\n\r\n#>\r\n\r\n&#91;cmdletbinding()]\r\nparam (\r\n\t&#91;parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]\r\n\t&#91;string&#91;]] $ComputerName = $env:computername\r\n)\r\n\r\nbegin {}\r\nprocess {\r\n\tforeach($Computer in $ComputerName) {\r\n\t\tWrite-Verbose \"Working on $Computer\"\r\n\t\tif(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {\r\n\t\t\t\r\n\t\t\ttry {\r\n\t\t\t\t$Networks = Get-WmiObject -Class Win32_NetworkAdapterConfiguration `\r\n\t\t\t\t\t\t\t-Filter IPEnabled=TRUE `\r\n\t\t\t\t\t\t\t-ComputerName $Computer `\r\n\t\t\t\t\t\t\t-ErrorAction Stop\r\n\t\t\t} catch {\r\n\t\t\t\tWrite-Verbose \"Failed to Query $Computer. Error details: $_\"\r\n\t\t\t\tcontinue\r\n\t\t\t}\r\n\t\t\tforeach($Network in $Networks) {\r\n\t\t\t\t$DNSServers = $Network.DNSServerSearchOrder\r\n\t\t\t\t$NetworkName = $Network.Description\r\n\t\t\t\tIf(!$DNSServers) {\r\n\t\t\t\t\t$PrimaryDNSServer = \"Notset\"\r\n\t\t\t\t\t$SecondaryDNSServer = \"Notset\"\r\n\t\t\t\t} elseif($DNSServers.count -eq 1) {\r\n\t\t\t\t\t$PrimaryDNSServer = $DNSServers&#91;0]\r\n\t\t\t\t\t$SecondaryDNSServer = \"Notset\"\r\n\t\t\t\t} else {\r\n\t\t\t\t\t$PrimaryDNSServer = $DNSServers&#91;0]\r\n\t\t\t\t\t$SecondaryDNSServer = $DNSServers&#91;1]\r\n\t\t\t\t}\r\n\t\t\t\tIf($network.DHCPEnabled) {\r\n\t\t\t\t\t$IsDHCPEnabled = $true\r\n\t\t\t\t}\r\n\t\t\t\t\r\n\t\t\t\t$OutputObj  = New-Object -Type PSObject\r\n\t\t\t\t$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()\r\n\t\t\t\t$OutputObj | Add-Member -MemberType NoteProperty -Name PrimaryDNSServers -Value $PrimaryDNSServer\r\n\t\t\t\t$OutputObj | Add-Member -MemberType NoteProperty -Name SecondaryDNSServers -Value $SecondaryDNSServer\r\n\t\t\t\t$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled\r\n\t\t\t\t$OutputObj | Add-Member -MemberType NoteProperty -Name NetworkName -Value $NetworkName\r\n\t\t\t\t$OutputObj\r\n\t\t\t\t\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\tWrite-Verbose \"$Computer not reachable\"\r\n\t\t}\r\n\t}\r\n}\r\n\r\nend {}\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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\u2019s 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45,59,48,63,74],"tags":[],"class_list":["post-3173","post","type-post","status-publish","format-standard","hentry","category-domain-name-system-dns","category-powershell","category-microsoft-windows-server-2012","category-server-2016-2016","category-windows-server-2019"],"_links":{"self":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3173"}],"version-history":[{"count":7,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3173\/revisions"}],"predecessor-version":[{"id":3180,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3173\/revisions\/3180"}],"wp:attachment":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}