Check if hostname exists in DNS

Darco
Site Admin
Posts: 80
Joined: Tue Jan 09, 2007 10:03 pm

Check if hostname exists in DNS

Postby Darco » Wed Jun 08, 2011 6:21 am

Hello,

Hereby a small script which check if a hostname exists in DNS.

Code: Select all

function verify_host
{
$erroractionpreference = "SilentlyContine"
try {
   ([System.Net.Dns]::GetHostAddresses($strTargetComputerAccount.split('.')[0]))[0].ipaddresstostring
}
catch {
   $exist = [System.Uri]::CheckHostName($_)
   if ($exist -ne 'IPv4'){
      Write-Host "Sorry, but this host doesn't exists in DNS" -foregroundcolor red -backgroundcolor yellow
      exit
   }
}
}

$strTargetComputerAccount =$Args[0].ToUpper()

verify_host


Always usefull to check when running scripts :lol:

Return to “PowerShell”

Who is online

Users browsing this forum: No registered users and 1 guest