none
DNS Script - Powershell

    Question

  • I'm busy working on a script to DNS check over 300 servers.

    My script does the following:

    1. Checks if there is a DNS record from a text file of hosts
    2. Returns to an excel file, the hostname, the Hostname in DNS, the ip address and eventually i want to add aliases.

    My script is as follows:

    $erroractionpreference = "SilentlyContinue"
    $a = New-Object -comobject Excel.Application
    $a.visible = $True

    $b = $a.Workbooks.Add()
    $c = $b.Worksheets.Item(1)

    $c.Cells.Item(1,1) = "Machine Name"
    $c.Cells.Item(1,2) = "DNS Entry"
    $c.Cells.Item(1,3) = "IP Address listed"

    $d = $c.UsedRange
    $d.Interior.ColorIndex = 19
    $d.Font.ColorIndex = 11
    $d.Font.Bold = $True
    $d.EntireColumn.AutoFit($True)

    $intRow = 2

    $colComputers = get-content C:\ScriptData\activehosts.txt
    foreach ($strComputer in $colComputers)
    {
    $c.Cells.Item($intRow, 1) = $strComputer.ToUpper()

    #Resolve the host name

    $dnsresult = [System.Net.DNS]::GetHostEntry($strComputer)

    #Display information about host

    #Give hostname Entry in Cell2

    $c.Cells.Item($intRow, 2) = $dnsresult.HostName

    #IP listed in Cell 3

    $c.Cells.Item($intRow, 3) = $dnsresult.AddressList[0].IpAddressToString



    $intRow = $intRow + 1

    }
    $d.EntireColumn.AutoFit()

    It is almost working perfectly, except that if there isn't a DNS record, it seems to just populate that field with the previous dns record it had from the previous server it looked up. I'm unsure what command to use to give a false value or just to output "No DNS record" in the hostname column and leave the ip column blank.

    Any advice is appreciated
    • Edited by Nick Colyer Tuesday, November 24, 2009 7:48 PM added scripting language to the heading
    Tuesday, November 24, 2009 5:23 PM

Answers

All replies

  • If the server is not found GetHostEntry returns an ArgumentException since you have it set to silently continue  I would assume that the assignment never happens therefore it would have whatever record was last successfully assigned.  you should put a trap in that puts some value indicative of a failure. 
    • Proposed as answer by rerun Tuesday, November 24, 2009 5:55 PM
    Tuesday, November 24, 2009 5:55 PM
  • Yeah i think that's where I'm stuck.

    Can anyone give me an example of how i put this trap in?

    If something fails $dnsresult = "fail"

    I'm just not sure what to put for the "If something fails" bit...
    Tuesday, November 24, 2009 7:43 PM
  • I believe this will work (using try/catch in V2)



    try {$dnsresult = [System.Net.DNS]::GetHostEntry($strComputer)}
    catch {$dnsresult = "Fail"}
    Tuesday, November 24, 2009 10:14 PM
    Moderator
  • Thanks,

    I ended up changing my script to the following:

    However I'm noticing that some of my hosts just come back with the hostname without any DNS suffix...Does this mean they are resolving from WINS?

    Lets take an example:

    Say we have a Host called "TESTHOST" with an ip of 10.10.10.10

    My columns come back with

    TestHost, TestHost, 10.10.10.10, Online

    After checking by in CMD by typing nslookup Testhost it comes back with nothing

    But I can ping it using ping testhost - so i'm guessing it is resolving from WINS?

    I wonder if i can modify my script to state this somehow?

    $erroractionpreference = "SilentlyContinue"
    $a = New-Object -comobject Excel.Application
    $a.visible = $True

    $b = $a.Workbooks.Add()
    $c = $b.Worksheets.Item(1)

    $c.Cells.Item(1,1) = "Machine Name"
    $c.Cells.Item(1,2) = "DNS Entry"
    $c.Cells.Item(1,3) = "IP Address listed"
    $c.Cells.Item(1,4) = "Ping check"

    $d = $c.UsedRange
    $d.Interior.ColorIndex = 19
    $d.Font.ColorIndex = 11
    $d.Font.Bold = $True
    $d.EntireColumn.AutoFit($True)

    $intRow = 2

    $colComputers = get-content C:\ScriptData\activehosts.txt
    foreach ($strComputer in $colComputers)
    {
    $c.Cells.Item($intRow, 1) = $strComputer.ToUpper()

    #Resolve the host name

    $dnsresult = [System.Net.DNS]::GetHostEntry($strComputer)

    if (!$?) {     $name="Host Not Resolved"
                $ip="Host Not Resolved"} else     {        
                                                $name = $dnsresult.hostname
                                                $ip = $dnsresult.AddressList[0].IpAddressToString
                                                }

    $ping = new-object System.Net.NetworkInformation.Ping
    $Reply = $ping.send($strComputer)
    if ($Reply.status –eq “Success”)
    {
    $c.Cells.Item($intRow, 4) = “Online”
    }
    else
    {
    $c.Cells.Item($intRow, 4) = "Offline"
    }
    $Reply = ""
                                               

    #Display information about host

    #Give hostname Entry in Cell2

    $c.Cells.Item($intRow, 2) = $name

    #IP listed in Cell 3

    $c.Cells.Item($intRow, 3) = $ip

    $intRow = $intRow + 1
    }
    $d.EntireColumn.AutoFit()
    Tuesday, November 24, 2009 10:39 PM
  • I believe you are indeed getting a WINS resolution on the entries that don't have a domain part.  Since the dot (.) is reserverd, no hostname will ever have one, so:

    if (!$?) {     $name="Host Not Resolved"
                $ip="Host Not Resolved"} else     {        
                                                $name = $dnsresult.hostname
                                                if ($name -notlike "*.*"){$name += " (from WINS)"}
                                                $ip = $dnsresult.AddressList[0].IpAddressToString
                                                }

    Tuesday, November 24, 2009 10:57 PM
    Moderator
  • I'm wondering if I can add something to this script, to also check if the Server belongs to a domain, and return the domain name it belongs to? Or if it belongs to a workgroup, report no domain?

    Is this something that can be done in powershell?

    Thanks,

    Nick
    Friday, January 22, 2010 3:58 PM
  • Nevermind I figured it out:


    $wmicomp = Get-WmiObject Win32_ComputerSystem
    $domain = $wmicomp.domain
    $c.cells.Item($introw, 5) = $domain

    Added in!
    Friday, January 22, 2010 5:06 PM
  • Awesome script.  I find it useful.


    Since many of my systems have IIS6 and IIS7 websites on them, how would I also get that info within this script?  How would I query DNS for this?

    Friday, June 10, 2011 7:19 PM
  • $IPAddr = [system.net.dns]::GetHostEntry($strComputer).addresslist

    $IPAddr[0]

    Gives you this:

     

    Address           : 3688585994
    AddressFamily     : InterNetwork
    ScopeId           :
    IsIPv6Multicast   : False
    IsIPv6LinkLocal   : False
    IsIPv6SiteLocal   : False
    IsIPv6Teredo      : False
    IPAddressToString : 10.1.9.219 # address changed to protect the inocent
    SortableAddress   : 173792219

    So if you know which of these you want, just type $IPAddr.IsIPv6???????? # whichever of the four you are looking for.

    Friday, June 10, 2011 8:45 PM