Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

dns.ps1 - powershell script by ActiveXperts Software

dns.ps1 query a DNS server, and check the response.

Use dns.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select dns.ps1. Configure the required parameter, or press 'Load a working sample'.

In ActiveXperts Network Monitor, Administrators can use three different scripting languages: Powershell, VBScript and SSH.


dns.ps1 script code

#################################################################################
# ActiveXperts Network Monitor PowerShell script, © ActiveXperts Software B.V.
# For more information about ActiveXperts Network Monitor, visit the ActiveXperts 
# Network Monitor web site at http://www.activexperts.com
#################################################################################
# Script
#     DNS.ps1
# Description: 
#     Query a DNS server, and validate the response
#     This function uses ActiveXperts Network Component.
#     ActiveXperts Network Component is automatically licensed when ActiveXperts Network Monitor is purchased.
#     For more information about ActiveXperts Network Component, see: www.activexperts.com/network-component
# Declare Parameters:
#     1) strHost (string) - Host name or IP address of the (remote) DNS server
#     2) strTarget (string) - Hostname or domain to query
#     3) strExpectedValue (int) - expected value 
# Usage:
#     .\DNS.ps1 '<Hostname | IP>' '<host>' '<expected value>'
# Sample:
#     .\DNS.ps1 'ns1.ascio.net' 'em1.activexperts-labs.com' '212.97.55.136'
#################################################################################

# -- Declare Parameters
param( [string]$strHost = '', [string]$strTarget = '', [string]$strExpectedValue = '' )

# -- Use _activexperts.ps1 with common functions 
. 'C:\Program Files\ActiveXperts\Network Monitor\Scripts\Monitor (ps1)\_activexperts.ps1' 


#################################################################################
# // --- Main script ---
#################################################################################

# -- Clear screen and clear error
cls
$Error.Clear()

# -- Validate parameters, return on parameter mismatch
if( $strHost -eq '' -or $strTarget -eq '' -or $strExpectedValue -eq '' )
{
  $res = 'UNCERTAIN: Parameter error - Usage: .\DNS.ps1 "<Hostname | IP>" "<host>" "<expected value>"'
  echo $res
  exit
}

$sysData = ''
$bMatched = $false

# -- Create object
$objDnsServer = new-object -comobject AxNetwork.DnsServer
$objConstants = new-object -comobject AxNetwork.NwConstants

# -- Lookup
$objDnsServer.Server = $strHost
$objDnsServer.Lookup( $strTarget, $objConstants.nwDNS_TYPE_A )

if( $objDnsServer.LastError -ne 0 )
{
  $res = 'UNCERTAIN: Unable to connect to query [' + $strHost + '] DATA:0 '
  echo $res
  exit
}  

$objDnsRecord = $objDnsServer.GetFirstRecord()

while( $objDnsServer.LastError -eq 0 )
{
  if( $objDnsRecord.Name -eq $strTarget ) 
  {
    if( $sysData -ne '' )
    {
      $sysData = $sysData + '; '
    }  
    
    $sysData = $sysData + $objDnsRecord.Address
    if( $objDnsRecord.Address -eq $strExpectedValue )
    {
      $bMatched = $true
    }
  }
  $objDnsRecord = $objDnsServer.GetNextRecord()
}
  
if( $bMatched -eq $true )
{
  $res = 'SUCCESS: Response matched; response = [' + $sysData + '] DATA:' + $sysData
}  
else
{
  $res = 'ERROR: Response did not match; response = [' + $sysData + '] DATA:' + $sysData
}  

# -- Print script result
echo $res;
exit


#################################################################################
# // --- Catch script exceptions ---
#################################################################################

trap [Exception]
{
  $res = 'UNCERTAIN: ' + $_.Exception.Message
  echo $res
  exit
}