PowerShell Monitor Script to query DNS Server

Topics: Developer Forum, News & Releases, User Forum
Apr 9, 2007 at 8:10 PM
I wrote a PowerShell script to query a DNS server for a DNS host. We are using this as a tool to go above and beyond testing that the DNS port or DNS service is active and running. This script will ensure that the DNS server is active and running and returning results for our SOA domains.

The current script executes a nslookup and parses the reults from the command line. Unfortunately, it seems the .NET Framework does not support performing DNS queries to a specific DNS server. I would have preffered to use an object model in PowerShell to perform these tests. The downside to nslookup is that each domain listed in the script will call a cmd shell session to execute the nslookup query, so there is probalby a lot more overhead than needed with this script.

Rob Philpott has a .NET DNS query component availble on Code Project that you may be able to use to replace nslookup.
http://www.codeproject.com/cs/internet/dnslookupdotnet.asp

There is a another related project on Code Project that could be useful as well.
http://www.codeproject.com/cs/internet/DNSTester.asp

Below is the script that we are using:




$myhosts = "domain1.com","domain2.com"
$server = "mydnsserver"
$err = 1

foreach ($hst in $myhosts)
{

$cmd = "nslookup " + $hst + " " + $server

$result = Invoke-Expression ($cmd)

if ($result3 -eq $null)
{
#"Host Not Found - " + $hst
if ($err -eq 1)
{
$err = 3
$Status.StatusID=3
$Status.StatusText="Host(s) Not Found: " + $hst
}
else
{
$Status.StatusText=$Status.StatusText +", " + $hst
}
}
Else
{
#"Host Found - " + $hst
if ($err -eq 1)
{
$Status.StatusID=1
$Status.StatusText="All Host(s) Found"
}
}

}