Poshnet DNS CMDLet and DNS lookup script

I was recently tasked with writing a script to resolve a few domains.  I hit the internet and found the perfect cmdlet posted by Joel Bennett.   The original code was posted on www.codeproject.com  which Joel converted to a cmdlet.  Thanks for having smart people willing to share stuff like this.  My script is an attempt to give back and how I used it. 


My script accepts a domain, resolves the IP(s) used in DNS, runs an HTTP Get on the host name and / or hostname with WWW.  It returns a status code, the AbsoluteURI.  This is handy when you are processing 100’s of domains trying to determine if they are valid.  PS – One of my favorite features is the EmulateDig option.   DIG is my favorite tool for DNS stuff. Thanks Joel!


#Here is the script

param
(
# [String] $DNSConfigFileList = $(throw “Please specify the external text file of domain names to lookup”),
#Sample Syntax

 #.Get-DNSLookup.ps1 www.iislogs.com C:TempLog.txt 192.168.1.40
 [String] $Domain = $(throw “Please specify the domain to lookup”),
 [String] $DNSLogFileAndPath = $(throw “Please specify the path where you want results logged”),
 [String] $DNSServer = $(throw “Please specify the DNS Server used for performing external queries. i.e It has to be an ip address, not server name”)
)


#Example of using a text file of domain names
#$DomainList = Get-Content C:TempCompleteList.txt


function Log-Information
{


 param
 (
  $Information
 )


 Add-Content $DNSLogFileAndPath $Information
}


function Get-DNSRecords
{
 param
 (
  $Domain,
  $DNSQueryResult
 )
 if($DNSQueryResult.Error.Length -ne 0)
 {
  Log-Information “There was an error with $($Domain)”
 }
 
 $DNSPublicationCount = $DNSQueryResult.Answers.Count
 if($DNSPublicationCount -eq 0)
 {
  $IsProblem = $true
  Log-Information “$($Domain):DNSPublicationCount is zero”
 }
 else
 {
  $arrIPs = “$($Domain)”
  for($i=0; $i -lt $DNSPublicationCount; $i++)
  { 
   
   $DNSIPA = $DNSQueryResult.Answers[$i].Record
   $DNSIPB = $DNSIPA.Address.IPAddressToString
   
   if($DNSIPA.Address.IPAddressToString -ne $null)
   {
    $arrIPs += “,$($DNSIPB)”
   }  
  }
  
  $WebResult = Get-WebResponse $Domain
  Log-Information “$($arrIPs),$($WebResult)”
 } 
}


function Get-WebResponse
{
 param
 (
  [string]$URL
 )


 #Try the host name first
 try
 {
  $Request = [System.Net.WebRequest]::Create(“http://$URL“)
  $Response = $request.GetResponse()
  $RequestStream = $response.GetResponseStream()
  $ReadStream = new-object System.IO.StreamReader $requestStream
  $Results = $readStream.ReadToEnd()
  $ReadStream.Close()
  $Response.Close()


  $AbsoluteUri = $Response.ResponseUri.AbsoluteUri
  $StatusCode = $Response.StatusCode.value__
  return “$($StatusCode),$($AbsoluteUri)”
 }
 catch
 {
  #Try with the WWW domain
  try
  {
   $Request = [System.Net.WebRequest]::Create(“http://www.$URL“)
   $Response = $request.GetResponse()
   $RequestStream = $response.GetResponseStream()
   $ReadStream = new-object System.IO.StreamReader $requestStream
   $Results = $readStream.ReadToEnd()
   $ReadStream.Close()
   $Response.Close()


   $AbsoluteUri = $Response.ResponseUri.AbsoluteUri
   $StatusCode = $Response.StatusCode.value__
   return “$($StatusCode),$($AbsoluteUri)”
  }
  catch
  {
   return “$($StatusCode),$($AbsoluteUri)”
  }  
 }
}


function Clear-DNSCache
{
 $command = “c:WindowsSystem32dnscmd.exe /clearcache”
 Log-Information “running command:$command”
 Invoke-Expression $command
}


#Load the PoshNet Get-DNS snapin
#Make sure to download Poshnet CMD from
#
http://huddledmasses.org/powershell-dig-nslookup-cmdlet/ 
#You need to install before running this script


Add-PSSnapIn PoshNet


#Clear DNS Cache on the server, assuming it’s local to the machine running the script
#The DNS service is assumed to be run local


Clear-DNSCache


#Loop through the domains on the
#foreach($Domain in $DomainList)
#{
# $DNSQueryResult = Get-Dns -Name $Domain -DnsServers $DNSServer
# Get-DNSRecords $Domain $DNSQueryResult
#}



#Run a single query against one domain


$DNSQueryResult = Get-Dns -Name $Domain -DnsServers $DNSServer
Get-DNSRecords $Domain $DNSQueryResult


Write-Host “Done”