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
Post a Comment