{"id":54333,"date":"2009-02-23T21:16:00","date_gmt":"2009-02-23T21:16:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/02\/23\/hey-scripting-guy-how-do-i-check-host-name-records\/"},"modified":"2009-02-23T21:16:00","modified_gmt":"2009-02-23T21:16:00","slug":"hey-scripting-guy-how-do-i-check-host-name-records","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-do-i-check-host-name-records\/","title":{"rendered":"Hey, Scripting Guy! How Do I Check Host Name Records?"},"content":{"rendered":"<h2><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/h2>\n<p>Hey, Scripting Guy! We are having name resolution issues at work. It is so bad that when my wife calls me, the guy in the other cube gets the call. This manifests itself in concrete ways as well. Opening Office Outlook seems to take forever; going to internal Web sites takes a long time; trying to connect to an Office SharePoint site is about impossible. To make matters worse, the pointy headed boss (PHB) just spent a small fortune on buying new switches. The project was supposed to &#8220;make the network fast,&#8221; but he never bothered to talk to us first. After five minutes with Netmon, it was obvious what was happening. Well, the good news is after we get DNS fixed, we really will have a fast network. The first thing I want to check are the A (host name) records. You got a script to do that?<\/p>\n<p>&#8211; JE<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><\/p>\n<p>Hi JE,<\/p>\n<p>A person after my own heart! I wrote the book on Netmon. Literally. Before I got into scripting, I used to do lots of network stuff. By the way, did you know we released <a href=\"http:\/\/blogs.technet.com\/netmon\/archive\/2008\/09\/17\/network-monitor-3-2-has-arrived.aspx\" target=\"_blank\">Network Monitor<\/a> (Netmon) 3.2 not too long ago? DNS problems often masquerade as network problems because we often need to find the host providing the information. If we only have a name, we need to translate the name into an IP address, but if it takes a long time to make that translation, even the fastest network in the world will seem like an old fashioned dial-up modem. <\/p>\n<table id=\"E3C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">This week we are talking about DNS. For more information about DNS, you can refer to the Windows Server 2008 Networking and Network Access Protection (NAP) book in the <a href=\"http:\/\/www.microsoft.com\/learning\/en\/us\/Books\/10345.aspx\" target=\"_blank\">Windows Server 2008 Resource Kit<\/a>. There is also a chapter on scripting network services in the <a href=\"http:\/\/www.microsoft.com\/MSPress\/books\/authors\/auth9541.aspx\" target=\"_blank\">Windows PowerShell Scripting Guide<\/a>. A number of good VBScript examples are in the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/network\/dns\/default.mspx\" target=\"_blank\">Script Center Script Repository<\/a>. The WMI DNS provider is documented here <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms682134(VS.85).aspx\" target=\"_blank\">on MSDN<\/a>.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Because you want to check out A records, take a look at the <b>GetDnsARecords.ps1<\/b> script. There is a VBScript version of this script in the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/network\/dns\/records\/nwrevb19.mspx\" target=\"_blank\">Script Center Script Repository<\/a>.<\/p>\n<p><b>GetDnsARecords.ps1<\/b><\/p>\n<pre class=\"codeSample\">Function Get-DnsServer\n{\n Begin {Write-Host -ForeGroundColor Cyan \"Obtaining local DNS Server information...\"}\n Process {\n  (Get-WmiObject -Class `\nwin32_networkadapterconfiguration -filter \"ipenabled =   $true\").DnsServerSearchOrder[0]\n } #end Process\n} #end Get-DnsServer\nFunction Get-DomainName\n{\n Begin {Write-Host -ForeGroundColor DarkCyan \"Obtaining local DNS Domain information...\"}\n Process {\n  (Get-WmiObject -Class win32_networkadapterconfiguration -filter \"ipenabled =   $true\").DnsDomain\n } #end Process\n} #end Get-DnsServer\nFunction Get-ARecords($DnsServer)\n{\n Begin { Write-Host -ForeGroundColor Yellow \"Connecting to $DnsServer ...\" }\n Process { Write-Host -ForeGroundColor Green \"Retrieving A records ...\"\n  Get-WmiObject -Class MicrosoftDNS_AType -NameSpace Root\\MicrosoftDNS\n  -ComputerName $DnsServer  -Filter \"DomainName = 'nwtraders.com'\" |\n  Select-Object -property Ownername, ipaddress\n } #end process\n} #end Get-ARecords\n# *** Entry Point to Script ***\n$DnsServer = Get-DnsServer\n$DnsDomain = Get-DomainName\nGet-ARecords -DnsDomain $DnsDomain -DnsServer $DnsServer\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The first thing we need to do is to create a function that will obtain the address of the DNS server. A good name for such a function is <b>Get-DnsServer<\/b>. To create a function, we use the function keyword and give it a name. There are no inputs required for the function because it will simply query the TCP\/IP configuration information on the local computer.<\/p>\n<pre class=\"codeSample\">Function Get-DnsServer\n{\n<\/pre>\n<p>We are going to now use a feature of a function that we have not previously written about\u2014the <b>Begin<\/b> parameter for a function. The <b>Begin<\/b> parameter runs once for items on the pipeline. Most of the time, we are using the process parameter for a function. It is, in fact, the default parameter for a function and as a result is normally left off.<\/p>\n<p>We normally do not like relying on default parameters. There are several reasons for this, but the biggest reason is that we like to know what a script is doing. If we do not know that we are using a default parameter, we may not understand when the script fails when we attempt a different parameter. One other reason is that if we do not know we are using a default parameter, we may be similarly unaware of the presence of other parameters.<\/p>\n<p>The parts of a function are seen in <b>DemoFunctionParameters.ps1<\/b>. An array of numbers is created by using the <b>1..4<\/b> notation. This creates an array of 4 numbers: 1,2,3,4. This array is pipelined to the <b>test<\/b> function. The first thing that happens is the display of the message, &#8220;Beginning test&#8221;. Then each number that comes across the pipeline is added to the number 1, and the results are displayed. When the process block is completed, the last thing that happens is the display of the message, \u201cCompleted test\u201d. The result of running the <b>DemoFunctionParameters.ps1<\/b> script is seen here:<\/p>\n<pre class=\"codeSample\">Beginning test\n2\n3\n4\n5\nCompleted test\n<\/pre>\n<p>The complete <b>DemoFunctionParameters.ps1<\/b> script is seen here:<\/p>\n<pre class=\"codeSample\">Function test\n{\n Begin {\"Beginning test\" }\n Process {$_ + 1 }\n End { \"Completed test\" }\n} #end test\n1..4 | test\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Back to our real script. The first thing we do is use the <b>Begin<\/b> statement list to print out a message in Cyan that informs the user that we are obtaining local DNS server information. This is done by using the <b>Write-Host<\/b> cmdlet with the <b>ForegroundColor<\/b> parameter. The <b>Begin<\/b> statement list is seen here:<\/p>\n<pre class=\"codeSample\">Begin {Write-Host -ForeGroundColor Cyan \"Obtaining local DNS Server information...\"}<\/pre>\n<p>When we have produced the first status message, it is time to obtain the DNS server information. We can get this information from the <b>Win32_NetworkAdapterConfiguration<\/b> WMI class. (Here\u2019s <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/networking\/default.mspx\" target=\"_blank\">a good series of articles on scripting networking<\/a>. They are all VBScript examples, but the techniques remain the same.) For additional information about working with network adapters using Windows PowerShell, you can see <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept08\/hey0908.mspx\" target=\"_blank\">this article<\/a>.<\/p>\n<p>With Windows PowerShell, the primary tool to use when working with WMI is the <b>Get-WmiObject<\/b> cmdlet. There are a number of parameters for this cmdlet, but the primary ones are seen in <b>Table 1<\/b>.<\/p>\n<table id=\"EKG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead>\n<tr>\n<td class=\"tableHeader\" colSpan=\"6\">Table 1 Get-WmiObject cmdlet<\/td>\n<\/tr>\n<tr class=\"stdHeader\" vAlign=\"top\">\n<td id=\"colENG\">Get-WmiObject<\/td>\n<td id=\"colERG\">Class<\/td>\n<td id=\"colEVG\">NameSpace<\/td>\n<td id=\"colEZG\">ComputerName<\/td>\n<td id=\"colE4G\">Filter<\/td>\n<td id=\"colEBH\">Credential<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">Cmdlet<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">WMI class<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">WMI Namespace<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Remote computername<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">WQL filter<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Credentials to use with remote connection<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>When the <b>Get-WmiObject<\/b> cmdlet is run, a management object is returned. If there is only one instance of that object, we can directly access properties of the object by using a dotted notation. In our script, we are querying the <b>Win32_NetworkAdapterConfiguration<\/b> WMI class. On most modern computers, there are nearly a dozen &#8220;things&#8221; that get reported as network adapters. Most computers only have one or two &#8220;real&#8221; network adapters, but everything from VPN connections to Bluetooth devices end up in the list. To return a single instance of a network adapter can often be a problem. One way that often works is to see if the adapter is IP enabled. That is, does it have an Internet Protocol stack enabled on it? This will filter out many of the bogus adapters.<\/p>\n<p>I am deliberately being obtuse here because if you have multiple network cards enabled on your computer, this function could very well fail because of the way it is written. Because there are so many different network configurations available among our readers, I want you to be aware of the potential limitation here.<\/p>\n<p>After we have the network adapter, we retrieve the IP address for the first DNS server. The <b>DnsServerSearchOrder<\/b> property is stored as an array. We are selecting the first one to query. The process section of the function is seen here (note that the second line should be one line of code; using the backtick character, we have broken it into two lines of code so that this page will not scroll horizontally in your browser):<\/p>\n<pre class=\"codeSample\">Process {\n(Get-WmiObject -Class `\nwin32_networkadapterconfiguration -filter \"ipenabled =   $true\").DnsServerSearchOrder[0]\n } #end Process\n} #end Get-DnsServer\n<\/pre>\n<p>Next we want to retrieve the domain name for the local computer. To do this, we will once again query the <b>Win32_NetworkAdapterConfiguration<\/b> WMI class. Once again we use the <b>Begin<\/b> statement list to print out a status message to indicate that we are obtaining local DNS domain information. This time, we print out the message in DarkCyan. By the way, if you are curious about which colors are available, you can use the <b>GetNames<\/b> static method from the <b>System.Enum<\/b> .NET Framework class, as seen here:<\/p>\n<pre class=\"codeSample\">PS C:\\&amp;gt; [enum]::GetNames(\"System.ConsoleColor\")\nBlack\nDarkBlue\nDarkGreen\nDarkCyan\nDarkRed\nDarkMagenta\nDarkYellow\nGray\nDarkGray\nBlue\nGreen\nCyan\nRed\nMagenta\nYellow\nWhite\n<\/pre>\n<p>The <b>Get Begin<\/b> statement list is seen here: <\/p>\n<pre class=\"codeSample\">Function Get-DomainName\n{\n Begin {Write-Host -ForeGroundColor DarkCyan \"Obtaining local DNS Domain information...\"}\n<\/pre>\n<p>It is time to create our <b>Process<\/b> block. The code is exactly the same as the previous <b>Process<\/b> block, except that this time we return the <b>DnsDomain<\/b> property. This will give us the domain name to use when we need to filter the results later on in the next function: <\/p>\n<pre class=\"codeSample\">Process {\n  (Get-WmiObject -Class win32_networkadapterconfiguration -filter \"ipenabled =   $true\").DnsDomain\n } #end Process\n} #end Get-DnsServer\n<\/pre>\n<p>Now we come to the <b>Get-ARecords<\/b> function. This function is used to retrieve A records from the DNS server. We once again use <b>Begin<\/b> to print out a status message. This is seen here:<\/p>\n<pre class=\"codeSample\">Function Get-ARecords($DnsServer)\n{\n Begin { Write-Host -ForeGroundColor Yellow \"Connecting to $DnsServer ...\" }\n<\/pre>\n<p>Then in the <b>Process<\/b> block, we write another status message (this time in green) that we are retrieving the A records, and we get to the WMI query. The WMI DNS provider resides in the <b>Root\\MicrosoftDNS<\/b> WMI namespace. To work with DNS, we need to specify that WMI namespace. The default WMI namespace is <b>Root\\CimV2<\/b>. <\/p>\n<p>Most of the WMI classes that are used by IT pros live in that namespace, and as a result many are unaware of the other WMI namespaces that exist. <\/p>\n<p>The following is a listing of DNS classes from my Windows Server 2008 computer: <\/p>\n<pre class=\"codeSample\">MicrosoftDNS_AAAAType\nMicrosoftDNS_AFSDBType\nMicrosoftDNS_ATMAType\nMicrosoftDNS_AType\nMicrosoftDNS_Cache\nMicrosoftDNS_CNAMEType\nMicrosoftDNS_Domain\nMicrosoftDNS_DomainDomainContainment\nMicrosoftDNS_DomainResourceRecordContainment\nMicrosoftDNS_HINFOType\nMicrosoftDNS_ISDNType\nMicrosoftDNS_KEYType\nMicrosoftDNS_MBType\nMicrosoftDNS_MDType\nMicrosoftDNS_MFType\nMicrosoftDNS_MGType\nMicrosoftDNS_MINFOType\nMicrosoftDNS_MRType\nMicrosoftDNS_MXType\nMicrosoftDNS_NSType\nMicrosoftDNS_NXTType\nMicrosoftDNS_PTRType\nMicrosoftDNS_ResourceRecord\nMicrosoftDNS_RootHints\nMicrosoftDNS_RPType\nMicrosoftDNS_RTType\nMicrosoftDNS_Server\nMicrosoftDNS_ServerDomainContainment\nMicrosoftDNS_SIGType\nMicrosoftDNS_SOAType\nMicrosoftDNS_SRVType\nMicrosoftDNS_Statistic\nMicrosoftDNS_TXTType\nMicrosoftDNS_WINSRType\nMicrosoftDNS_WINSType\nMicrosoftDNS_WKSType\nMicrosoftDNS_X25Type\nMicrosoftDNS_Zone\n<\/pre>\n<p>The WMI&nbsp;class we are going to query to obtain A records is the <b>MicrosoftDNS_AType<\/b>. These WMI class names are pretty easy to work with because they all begin with <b>MicrosoftDNS<\/b> and and underscore. If we wanted to list the MX records, the class would be <b>MicrosoftDNS_MXType<\/b>. In fact, you could easily replace the code we are using here with the DNS record type you were interested in obtaining. After specifying the WMI class name, we specify the namespace, which as you recall for DNS is <b>Root\\MicrosoftDNS<\/b>. Then the computername parameter is supplied. This is why we used the earlier function to obtain the name of the DNS server, because the <b>Root\\MicrosoftDNS<\/b> namespace does not exist unless the server is running DNS. However, using WMI we can connect to a namespace on a remote machine that does not exist on our local machine, and we can query WMI classes on a remote machine that do not exist on the local computer. <\/p>\n<p>On Windows 2000, you need to <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/dd348431.aspx\">download Dnsprov.zip<\/a> and use MOFCOMP to install the DNS provider. On Windows Server 2003 and later, it is installed automatically. <\/p>\n<p>We use the <b>Filter<\/b> parameter to limit the A records to those in our own domain. We then use the <b>Select-Object<\/b> cmdlet to retrieve the <b>Ownername<\/b> and the IP address. This is seen here: <\/p>\n<pre class=\"codeSample\">Process { Write-Host -ForeGroundColor Green \"Retrieving A records ...\"\n  Get-WmiObject -Class MicrosoftDNS_AType -NameSpace Root\\MicrosoftDNS -ComputerName\n$DnsServer  -Filter \"DomainName = 'nwtraders.com'\" |  Select-Object -property Ownername, ipaddress\n } #end process\n} #end Get-ARecords\n<\/pre>\n<p>Next we come to the entry point of the script. We call the <b>Get-DnsServer<\/b> function and store the DNS server name in the <b>$DnsServer<\/b> variable. We call the <b>Get-DomainName<\/b> function and store the domain name in the <b>$DnsDomain<\/b> variable. We then pass both of these values to the <b>GetARecords<\/b> function. This is seen here: <\/p>\n<pre class=\"codeSample\">$DnsServer = Get-DnsServer\n$DnsDomain = Get-DomainName\nGet-ARecords -DnsDomain $DnsDomain -DnsServer $DnsServer\n<\/pre>\n<p>The results of running the script are seen here: <\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Image of the results of running the script\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/february\/hey0223\/hsg-02-23-9-01.jpg\" width=\"500\" height=\"248\"><\/p>\n<p>&nbsp;<\/p>\n<p>Well, JE, that is all there is to using the DNS provider to let us find A records. Join us tomorrow as DNS week continues.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! We are having name resolution issues at work. It is so bad that when my wife calls me, the guy in the other cube gets the call. This manifests itself in concrete ways as well. Opening Office Outlook seems to take forever; going to internal Web sites takes a long time; trying [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[188,37,3,45],"class_list":["post-54333","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dns-server","tag-networking","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! We are having name resolution issues at work. It is so bad that when my wife calls me, the guy in the other cube gets the call. This manifests itself in concrete ways as well. Opening Office Outlook seems to take forever; going to internal Web sites takes a long time; trying [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=54333"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54333\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=54333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=54333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=54333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}