Skip to main content

PowerShell - To Perform DNS Resolution Check-Up

So this is my second PowerShell script created by me. The first one was so simple and I use it in my XenDesktop environment. This script on the other hand, is aprt of my initiative in performing Active Directory clean-up in my environment. 

The task of the script is simple - check the IP address of a machine name, then check the hostname of that IP address. 

- If the machine name (A host record) and the hostname (PTR record) is similar, we are good. 
- If the machine name (A host record) and the hostname (PTR record) is different, error prompted
- If the machine name (A host record) is available but the hostname (no PTR record) is not, error prompted
- If the machine is not avaiable (no A host record), error prompted.

I use $PSScriptRoot so the location of the script is dynamic, it does not necessarily need to be put at a specific location.

This is only part 1. I wish to add more features so it could be better next time.



############################################################
#
#       This script is created by Heiry Zulkifli, in order to check DNS resolutions for hostnames.
# www.datakraf.co.nz
#
#######################################################################

Function FunctChkIPAdd ($Machine)
{
Try
{
$arr = [System.Net.Dns]::GetHostAddresses($Machine)  | findstr "IPAddressToString"
$SplitArr = $arr -split ': '
$IPAdd = $SplitArr[1]
$ErrorCode = 0
RETURN $ErrorCode, $IPAdd
} Catch {
$ErrorCode = 1
RETURN $ErrorCode, $IPAdd
}
}
Function FunctChkHostname ($IPAdd)
{
Try
{
$arr = [System.Net.Dns]::GetHostbyAddress($IPAdd) | findstr "{}"
$SplitArr = $arr -split ' '
$FQDN = $SplitArr[0]
$SplitArr2 = $FQDN.Split('.')
$Hostname = $SplitArr2[0]
$ErrorCode = 0
RETURN $ErrorCode, $Hostname
} Catch {
$ErrorCode = 2
RETURN $ErrorCode, $Hostname
}
}
$strFileName = "$PSScriptRoot\result.txt"
if (Test-path $strFileName) {remove-item $strFileName}
"MACHINENAME IP ADDRESS HOSTNAME STATUS" >$strFileName
write-host ("Script starts...")
write-host ("Performing Flush DNS...")
ipconfig /flushdns
write-host ("Flush DNS completed")
write-host (".")
write-host (".")
write-host (".")

forEach ($Machine in get-Content $PSScriptRoot\machines.txt)
{
  write-host ("Checking $Machine")
$ResultIPAdd = FunctChkIPAdd ($Machine)
If ($ResultIPAdd[0] -eq 0)
{
$ResultHostName = FunctChkHostname ($ResultIPAdd[1])
$Result = $ResultHostName[0]
IF ($Result -eq 0)
{ $IPAdd = $ResultIPAdd[1]
$HostName = $ResultHostName[1]
IF ($Machine -eq $HostName)
{
$info = "$Machine $IPAdd $HostName | OK"
} ELSE {
$info = "$Machine $IPAdd $HostName---------------| ERROR - Maching Different With Hostname"
}
} ELSE {
$info = "$Machine $IPAdd -------------------------------------------| ERROR-HostName Not Found"
}
} ELSE {
$info = "$Machine ----------------------------------------------| ERROR-machine Name Not Found"
}
$info >> $strFileName
$hostname=""
write-host (".................................Completed")
}
write-host (".")
write-host (".")
write-host (".")
write-host ("Script completed. Please check result.txt")



When launches...




Example of result.txt




To summarize :
  • For storefront2012, the A Host and PTR record are similar, no problem.
  • For appsense2012, A Host is available, but PTR record is not available
  • For asdsfd123, both A Host and PTR record are not available
  • For VDA012012, A Host is pointing to 10.10.0.40, however the IP is pointing to different hostname.

Comments

Popular posts from this blog

Microsoft Assessment and Planning (MAP) Toolkit - Extract Report (3/4)

As mentioned in  the first post , this KB series is about Microsoft Assessment and Planning (MAP) Toolkit. There are 4 main steps : Install MAP Toolkit and its basic configuration Collect inventory Data  Extract Report Extract Advanced Report Once inventory data collected, we can generate reports. From the inventory data collected earlier, we can use options in the toolkit to generate the report. For this example, we re going to discover Windows 10 Readiness This KB is about  how to generate report from collected inventory data. At Overview page, select the targeted scenario category. In this example, it is Desktop . At this page, select specific scenario that we after. In this example, it is Windows 10 Readiness.     It is possible to customize assessment properties. The properties will set the threshold of the assessment, such as, threshold for minimum CPU speed, acceptable free disk, as well as minimum assigned RAM. To do so, s...

Microsoft Assessment and Planning (MAP) Toolkit - Collect Inventory Data (2/4)

As mentioned in  previous post , this KB series is about Microsoft Assessment and Planning (MAP) Toolkit. There are 4 main steps : Install MAP Toolkit and its basic configuration Collect inventory Data  Extract Report Extract Advanced Report Before we could generate a report, we need to collect inventory data. There are a few options, such as Windows computers, Linux computers, computers on VMware, Active Devices and users, etc. For this example, we re going to discover Windows Computers within a specific IP ranges. This KB is about  how to collect inventory data. At Overviews section, select Perform an inventory For this testing, let’s select Microsoft computers, and select Next For this testing, we will discover devices via IP range. Select scan an IP address range, and select Next Specify range if IPs, and select Next To discover and connect to the machines, we need to provide user account(s) that have permission to do so. Select ...

Microsoft Assessment and Planning (MAP) Toolkit - Overview

Microsoft created a  toolkit called Microsoft Assessment and Planning Toolkit that helps IT to discover current infrastructure for variety of migration projects.  This tool kit covers below scenarios : Windows computers Linux/UNIX computers VMware computers Active Directory and Users Exchange Servers Endpoint Protection Server Lync Server Softwre ID (SWID) tags SQL Server SQL Server with Database Details Microsoft Azure Platform Migration Oracle Windows Volume Licensing Client Access Tracking for Windows Server 2012 or Later Client Access Tracking for SQL Server 2012 or later Client Access Tracking for Configuration Manager Client Access Tracking for SharePoint Server 2016 Client Access Tracking for Remote Desktop Services … and IT Admins can use below methods to discover the machines. Use Active Directory Domain Service (AD DS) Use Windows networking protocols Use System Center Configuration Manager (SCCM) Scan an IP Address range Manually enter...