Last active
April 15, 2024 08:04
-
-
Save phyoewaipaing/e9daab8d3b9cf36eec11aeb16896c776 to your computer and use it in GitHub Desktop.
Enumerate DNS Records and check the existence on the Destination Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Script that will check the Replicated DNS Records between 2 DNS Servers. | |
.DESCRIPTION | |
** This script will check whether the dns records on primary server exists on secondary server. | |
** The script will only search these record types: A, PTR, CNAME, MX, SRV, but not NS, AAAA | |
** You can use this script if the 2 DNS Servers are out-of-sync with each different records | |
Example usage: | |
Check_DNS_Records_Status_v1.1.ps1 -DestinationServer myServer2.contoso.local | |
Author: Phyoe Wai Paing | |
Country: Myanmar(Burma) | |
Version : 1.0 : 03/30/2017 : Initial Release | |
Version : 1.1 : 05/22/2023 : Removed lines that check Server 2008 or Server 2012R2 and execution for server 2008 (because it's already expired). | |
Fixed PTR section. Thanks to Frank Dub <francois.dubois@gmail.com> for these commits. | |
.EXAMPLE | |
Check_DNS_Records_Status.ps1 -DestinationServer myServer2.contoso.local | |
This will check whether the DNS records (A PTR, CANAME, MX, SRV) on the primary server (the local server the script is running) | |
exists on the secondary server. | |
.PARAMETER DestinationServer | |
The secondary or destination server on which you check if the records on primary server exist. | |
.LINK | |
You can find this script and more at: https://www.scriptinghouse.com/ | |
#> | |
Param([string]$DestinationServer,) | |
$AllEnumeratedRecords = @() | |
Get-DnsServerZone | foreach { $ZoneName=$_.ZoneName; Get-DnsServerResourceRecord $_.ZoneName | where { $_.RecordType -ne 'NS' -AND $_.RecordType -ne 'SOA' } | foreach { | |
## If the record in the zone is PTR then create the object with the appropriate values ## | |
If ($_.RecordType -eq "PTR" -AND $_.DistinguishedName -ne '1.0.0') | |
{ | |
$LastOctetSplit = $_.DistinguishedName.split(',')[0].split('=')[1].Split('.') | |
$First3OctetSplit = $_.DistinguishedName.split(',')[1].split('=')[1].split('in-addr')[0].TrimEnd('.').Split('.') | |
[array]::reverse($First3OctetSplit) | |
[array]::reverse($LastOctetSplit) | |
$LastOctet = $LastOctetSplit -join '.' | |
$First3Octet = $First3OctetSplit -join '.' | |
$Value = $First3Octet+'.'+$LastOctet | |
$FQDN = $_.RecordData.PtrDomainName.TrimEnd('.') | |
$Obj = $_ | select RecordType,RecordData,TimeStamp -ExpandProperty TimeToLive | select * -ExpandProperty RecordData | select FQDN,Value,Recordtype,Priority,Port,Weight,TimeStamp, @{N="TTL";Exp = { $_.TotalSeconds}},FoundOnServer_2, ValueOnServer_2, Status | |
$Obj.FQDN = $FQDN | |
$Obj.Value = $Value | |
If ($Obj.Value -NotMatch '127.0.0.1') | |
{ | |
$AllEnumeratedRecords += $Obj | |
} ## Output the Value not equal to localhost | |
} | |
elseif ($_.RecordType -eq "SRV") | |
{ | |
$FQDN = $_.HostName + '.'+$ZoneName | |
$Obj = $_ | select RecordType,RecordData,TimeStamp -ExpandProperty TimeToLive | select * -ExpandProperty RecordData | select @{N="FQDN";Exp= {$FQDN}}, Recordtype,Priority,TimeStamp,@{N="TTL";Exp = { $_.TotalSeconds}},@{N="Value";Exp={$_.DomainName.TrimEnd('.')} }, FoundOnServer_2, ValueOnServer_2, Status | |
$Obj | Add-Member NoteProperty "Port" $_.RecordData.Port | |
$Obj | Add-Member NoteProperty "Weight" $_.RecordData.Weight | |
$AllEnumeratedRecords += $Obj | |
} | |
elseif ($_.RecordType -eq "MX") | |
{ | |
$Value = $_.RecordData.MailExchange.TrimEnd('.') | |
$Priority = $_.RecordData.Preference | |
$Obj = $_ | select RecordType,RecordData,TimeStamp -ExpandProperty TimeToLive | select * -ExpandProperty RecordData | select @{N="FQDN";Exp= {$ZoneName}}, Recordtype,Port,Weight,TimeStamp, @{N="TTL";Exp = { $_.TotalSeconds}}, FoundOnServer_2, ValueOnServer_2, Status | |
$Obj | Add-Member NoteProperty "Value" $Value | |
$Obj | Add-Member NoteProperty "Priority" $Priority | |
$AllEnumeratedRecords += $Obj | |
} | |
elseif ($_.RecordType -eq "A" -AND $_.HostName -ne "@") | |
{ | |
$FQDN = $_.HostName+'.'+$ZoneName | |
$Value = $_.RecordData.IPv4Address.IPAddressToString | |
$Obj = $_ | select RecordType,RecordData,TimeStamp -ExpandProperty TimeToLive | select * -ExpandProperty RecordData | select @{N="FQDN";Exp= {$FQDN}}, RecordType,Priority,Port,Weight, TimeStamp, @{N="TTL";Exp = { $_.TotalSeconds}}, FoundOnServer_2, ValueOnServer_2, Status | |
$Obj | Add-Member NoteProperty "Value" $Value | |
$AllEnumeratedRecords += $Obj | |
} | |
elseif ($_.RecordType -eq "CNAME") | |
{ | |
$FQDN = $_.HostName+'.'+$ZoneName | |
$Value = $_.RecordData.HostNameAlias.TrimEnd('.') | |
$Obj = $_ | select RecordType,RecordData,TimeStamp -ExpandProperty TimeToLive | select * -ExpandProperty RecordData | select @{N="FQDN";Exp= {$FQDN}}, RecordType,Priority,Port,Weight, TimeStamp, @{N="TTL";Exp = { $_.TotalSeconds}},FoundOnServer_2, ValueOnServer_2, Status | |
$Obj | Add-Member NoteProperty "Value" $Value | |
$AllEnumeratedRecords += $Obj | |
} | |
} | |
} | |
#$AllEnumeratedRecords ######+++++++++++++++++++++++++++++++++++++++++++++++++++++ Output all enumerated Records here | |
$PtrLookupResultObj =@() | |
$SrvLookupResultObj =@() | |
$MxLookupResultObj =@() | |
$ALookupResultObj =@() | |
$CnameLookupResultObj = @() | |
#######----------------------------------------------------- Compare Section starts here ---------------------------------------------------------########## | |
$AllEnumeratedRecords | foreach { | |
#################------------------------------------------- PTR compare section starts here ----------------------------------##################### | |
If ($_.RecordType -eq "PTR") | |
{ | |
$NotFound = 0; | |
Try { | |
$PtrLookupResultObj = Resolve-DnsName -Type PTR $_.Value -Server $DestinationServer -TcpOnly -DnsOnly -NoRecursion -EA Stop | |
$QueriedPtrRecordFQDN = $PtrLookupResultObj.NameHost | |
$PtrLookupResultObj = $PtrLookupResultObj | select @{N="FQDN";Exp = { $QueriedPtrRecordFQDN }}, TTL, Priority,Port,Weight | |
$PtrLookupResultObj | Add-Member NoteProperty "Value" $($_.Value) | |
$PtrLookupResultObj | Add-Member NoteProperty "Status" "OK" | |
$EnumeratedPTRFQDN = ($AllEnumeratedRecords | where { $_.RecordType -eq "PTR" } ).FQDN ## Find all the FQDNs of enumerated records | |
$ExtraRecordsDestination = 0; | |
$QueriedPtrRecordFQDN | foreach { If ( $EnumeratedPTRFQDN -contains $_ ) { } else { $ExtraRecordsDestination = 1; } } | |
If (!($QueriedPtrRecordFQDN -is [array])) | |
{ | |
If ($_.FQDN -eq $PtrLookupResultObj.FQDN) | |
{ $_.FoundOnServer_2 = "Found"; $_.ValueOnServer_2 = $QueriedPtrRecordFQDN; $_.Status = "OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
elseif ($PtrLookupResultObj.Status -eq "OK") | |
{ $_.FoundOnServer_2 = "Not Matched"; $_.ValueOnServer_2 = $QueriedPtrRecordFQDN; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
else | |
{ $_.FoundOnServer_2 = "Not Found"; $_.ValueOnServer_2 = $QueriedPtrRecordFQDN; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
} | |
else | |
{ | |
if ($QueriedPTRrecordFQDN -Contains $_.FQDN -AND $ExtraRecordsDestination) | |
{ $_.FoundOnServer_2 = "Found(Extra on Dest)"; $_.ValueOnServer_2 = $QueriedPTRrecordFQDN | Sort | Unique; $_.Status = "OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
elseIf ($QueriedPTRrecordFQDN -Contains $_.FQDN ) | |
{ $_.FoundOnServer_2 = "Found"; $_.ValueOnServer_2 = $QueriedPTRrecordFQDN | Sort | Unique; $_.Status = "OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
elseif ($QueriedPTRrecordStatus -eq "OK") | |
{ $_.FoundOnServer_2 = "Not Matched"; $_.ValueOnServer_2 = $QueriedPTRrecordFQDN | Sort | Unique; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
else | |
{ $_.FoundOnServer_2 = "Not Found"; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
} | |
} | |
Catch { | |
$NotFound = 1; ## We cannot pass the $_ value to catch statement, so we have to set the flag | |
If ([string]$NotRespond -match "did not properly respond after a period of time" ) | |
{ | |
Write-Host -fore Red "Invalid Destination Server: $DestinationServer`nPlease make sure the Destination's DNS Service is up & running and there is no firewall rule blocked."; exit; | |
} | |
} | |
## Fill the properties with "Not Found" value if the $NotFound is set by catch statement ## | |
If ($NotFound) | |
{ $_.FoundOnServer_2 = "Not Found"; $_.ValueOnServer_2 =""; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
} | |
#################------------------------------------------- PTR compare section Starts Here ----------------------------------##################### | |
#################------------------------------------------- SRV compare section Starts Here ----------------------------------##################### | |
elseif ($_.RecordType -eq "SRV") | |
{ | |
$NotFound = 0; | |
Try { | |
$SrvLookupResultObj = (Resolve-DnsName -Type SRV $_.FQDN -Server $DestinationServer -TcpOnly -DnsOnly -NoRecursion -EA Stop -ErrorVariable NotRespond) | ? { $_.Section -eq "Answer"} | |
$QueriedSrvRecordFQDN = $SrvLookupResultObj.Name | |
$QueriedSrvRecordValue = $SrvLookupResultObj.NameTarget | |
$SrvLookupResultObj = $SrvLookupResultObj | select @{N="FQDN";Exp = { $QueriedSrvRecordFQDN }}, TTL, Priority,Port,Weight | |
$SrvLookupResultObj | Add-Member NoteProperty "Value" $QueriedSrvRecordValue | |
$SrvLookupResultObj | Add-Member NoteProperty "Status" "OK" | |
$ToFindValue = $QueriedSrvRecordValue; $ToFindWeight = $SrvLookupResultObj.Weight; $ToFindPriority = $SrvLookupResultObj.Priority | |
$EnumeratedSrvValue = ($AllEnumeratedRecords | where { $_.RecordType -eq "SRV" } ).Value ## Find all the Values of enumerated records | |
$ExtraRecordsDestination = 0; | |
$QueriedSrvRecordValue | foreach { If ( $EnumeratedSrvValue -contains $_ ) { } else { $ExtraRecordsDestination = 1; } } | |
If (!($QueriedSrvRecordValue -is [array])) | |
{ | |
If (($_.Value -eq $ToFindValue) -AND ($_.Weight -eq $ToFindWeight) -AND ($_.Priority -eq $ToFindPriority )) | |
{ $_.FoundOnServer_2 = "Found"; $_.ValueOnServer_2 = $QueriedSrvRecordValue; $_.Status = "OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
elseif ($SrvLookupResultObj.Status -eq "OK") | |
{ $_.FoundOnServer_2 = "Not Matched"; $_.ValueOnServer_2 = $QueriedSrvRecordValue; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
else | |
{ $_.FoundOnServer_2 = "Not Found"; $_.ValueOnServer_2 = $QueriedSrvRecordValue; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
} | |
else | |
{ | |
if ($QueriedSrvRecordValue -Contains $_.Value -AND $ExtraRecordsDestination) | |
{ $_.FoundOnServer_2 = "Found(Extra on Dest)"; $_.ValueOnServer_2 = $QueriedSrvRecordValue | Sort | Unique; $_.Status = "OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
elseif ($QueriedSrvRecordValue -Contains $_.Value) | |
{ $_.FoundOnServer_2 = "Found"; $_.ValueOnServer_2 = $QueriedSrvRecordValue | Sort | Unique; $_.Status = "OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
elseif ($QueriedSrvRecordStatus -eq "OK") | |
{ $_.FoundOnServer_2 = "Not Matched"; $_.ValueOnServer_2 = $QueriedSrvRecordValue | Sort | Unique; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
else | |
{ $_.FoundOnServer_2 = "Not Found"; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
} | |
} | |
Catch { | |
$NotFound = 1; ## We cannot pass the $_ value to catch statement, so we have to set the flag | |
If ([string]$NotRespond -match "did not properly respond after a period of time" ) | |
{ | |
Write-Host -fore Red "Invalid Destination Server: $DestinationServer`nPlease make sure the Destination's DNS Service is up & running and there is no firewall rule blocked."; exit; | |
} | |
} | |
If ($NotFound) | |
{ $_.FoundOnServer_2 = "Not Found"; $_.ValueOnServer_2 =""; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
} | |
#################------------------------------------------- SRV compare section Ends Here ----------------------------------##################### | |
#################------------------------------------------- MX compare section Starts Here -----------------------------------##################### | |
elseif ($_.RecordType -eq "MX") | |
{ | |
$NotFound = 0; | |
Try { | |
$MxLookupResultObj = Resolve-DnsName -Type MX $_.FQDN -Server $DestinationServer -TcpOnly -DnsOnly -NoRecursion -EA Stop -ErrorVariable NotRespond | |
$QueriedMxRecordFQDN = $MxLookupResultObj.Name | |
$QueriedMxRecordValue = $MxLookupResultObj.NameExchange | |
$QueriedMxRecordPriority = $MxLookupResultObj.Preference | |
$QueriedMxRecordSection = $MxLookupResultObj.Section | |
$MxLookupResultObj = $MxLookupResultObj | select @{N="FQDN";Exp = { $QueriedMxRecordFQDN }}, {N="Priority"; Exp = { $QueriedMxRecordPriority }} , TTL, Port, Weight | |
$MxLookupResultObj | Add-Member NoteProperty "Value" $QueriedMxRecordValue | |
$MxLookupResultObj | Add-Member NoteProperty "Status" "OK" | |
$EnumeratedMxValue = ($AllEnumeratedRecords | where { $_.RecordType -eq "MX" } ).Value ## Find all the Values of enumerated records | |
$ExtraRecordsDestination = 0; | |
$QueriedMxRecordValue | foreach { If ( $EnumeratedMxValue -contains $_ ) { } else { $ExtraRecordsDestination = 1; } } | |
If (!($QueriedMxRecordValue -is [array])) | |
{ | |
If ( $_.Value -eq $QueriedMxRecordValue) | |
{ $_.FoundOnServer_2 = "Found"; $_.ValueOnServer_2 = $QueriedMxRecordValue; $_.Status = "OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
elseif ($MxLookupResultObj.Status -eq "OK" -AND $QueriedMxRecordSection -eq "Answer" ) | |
{ $_.FoundOnServer_2 = "Not Matched"; $_.ValueOnServer_2 = $QueriedMxRecordValue; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
else | |
{ $_.FoundOnServer_2 = "Not Found"; $_.ValueOnServer_2 = $QueriedMxRecordValue; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
} | |
else | |
{ | |
if ($QueriedMxRecordValue -Contains $_.Value -AND $ExtraRecordsDestination) | |
{ $_.FoundOnServer_2 = "Found(Extra on Dest)"; $_.ValueOnServer_2 = $QueriedMxRecordValue | Sort | Unique; $_.Status = "OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
elseIf ($QueriedMxRecordValue -Contains $_.Value ) | |
{ $_.FoundOnServer_2 = "Found"; $_.ValueOnServer_2 = $QueriedMxRecordValue | Sort | Unique; $_.Status = "OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
elseif ($MxLookupResultObj.Status -eq "OK") | |
{ $_.FoundOnServer_2 = "Not Matched"; $_.ValueOnServer_2 = $QueriedMxRecordValue | Sort | Unique; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
else | |
{ $_.FoundOnServer_2 = "Not Found"; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
} | |
} | |
Catch { | |
$NotFound = 1; ## We cannot pass the $_ value to catch statement, so we have to set the flag | |
If ([string]$NotRespond -match "did not properly respond after a period of time" ) | |
{ | |
Write-Host -fore Red "Invalid Destination Server: $DestinationServer`nPlease make sure the Destination's DNS Service is up & running and there is no firewall rule blocked."; exit; | |
} | |
} | |
If ($NotFound) | |
{ $_.FoundOnServer_2 = "Not Found"; $_.ValueOnServer_2 =""; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
} | |
#################------------------------------------------- MX compare section Ends Here -----------------------------------##################### | |
#################------------------------------------------- A compare section Starts Here -------------------------------------##################### | |
elseif ($_.RecordType -eq "A") | |
{ | |
$NotFound = 0; | |
Try { | |
$ALookupResultObj = Resolve-DnsName -Type A $_.FQDN -Server $DestinationServer -TcpOnly -DnsOnly -NoRecursion -ErrorVariable NotRespond -EA Stop | |
$QueriedARecordFQDN = $ALookupResultObj.Name | |
$QueriedARecordValue = $ALookupResultObj.IPAddress | |
$ALookupResultObj = $ALookupResultObj | select @{N="FQDN";Exp = { $QueriedARecordFQDN }},TTL,Priority,Port,Weight | |
$ALookupResultObj | Add-Member NoteProperty "Value" $QueriedARecordValue | |
$ALookupResultObj | Add-Member NoteProperty "Status" "OK" | |
$EnumeratedAValue = ($AllEnumeratedRecords | where { $_.RecordType -eq "A" } ).Value ## Find all the Values of enumerated records | |
$ExtraRecordsDestination = 0; | |
$QueriedARecordValue | foreach { If ( $EnumeratedAValue -contains $_ ) { } else { $ExtraRecordsDestination = 1; } } | |
If (!($QueriedARecordValue -is [array])) | |
{ | |
If ( $_.Value -eq $QueriedARecordValue) | |
{ $_.FoundOnServer_2 = "Found"; $_.ValueOnServer_2 = $QueriedARecordValue; $_.Status = "OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
elseif ($ALookupResultObj.Status -eq "OK" ) | |
{ $_.FoundOnServer_2 = "Not Matched"; $_.ValueOnServer_2 = $QueriedARecordValue; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
else | |
{ $_.FoundOnServer_2 = "Not Found"; $_.ValueOnServer_2 = $QueriedARecordValue; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
} | |
else | |
{ | |
if ($QueriedARecordValue -Contains $_.Value -AND $ExtraRecordsDestination) | |
{ $_.FoundOnServer_2 = "Found(Extra on Dest)"; $_.ValueOnServer_2 = $QueriedARecordValue | Sort | Unique; $_.Status = "OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status} | |
elseIf ($QueriedARecordValue -Contains $_.Value ) | |
{ $_.FoundOnServer_2 = "Found"; $_.ValueOnServer_2 = $QueriedARecordValue | Sort | Unique; $_.Status = "OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
elseif ($ALookupResultObj.Status -eq "OK") | |
{ $_.FoundOnServer_2 = "Not Matched"; $_.ValueOnServer_2 = $QueriedARecordValue | Sort | Unique; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
else | |
{ $_.FoundOnServer_2 = "Not Found"; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
} | |
} | |
Catch { | |
$NotFound = 1; ## We cannot pass the $_ value to catch statement, so we have to set the flag | |
If ([string]$NotRespond -match "did not properly respond after a period of time" ) | |
{ | |
Write-Host -fore Red "Invalid Destination Server: $DestinationServer`nPlease make sure the Destination's DNS Service is up & running and there is no firewall rule blocked."; exit; | |
} | |
} | |
If ($NotFound) | |
{ $_.FoundOnServer_2 = "Not Found"; $_.ValueOnServer_2 =""; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
} | |
#################------------------------------------------- A compare section Ends Here ---------------------------------------##################### | |
#################------------------------------------------ CNAME compare section Starts Here --------------------------------##################### | |
elseif ($_.RecordType -eq "CNAME") | |
{ | |
$NotFound = 0; | |
Try { | |
$CnameLookupResultObj = Resolve-DnsName -Type CNAME $_.FQDN -Server $DestinationServer -TcpOnly -DnsOnly -NoRecursion -EA Stop -ErrorVariable NotRespond | |
$QueriedCnameRecordFQDN = $CnameLookupResultObj.Name | |
$QueriedCnameRecordValue = $CnameLookupResultObj.NameHost | |
$QueriedCnameRecordSection = $CnameLookupResultObj.Section | |
$CnameLookupResultObj = $CnameLookupResultObj | select @{N="FQDN";Exp = { $QueriedCnameRecordFQDN }},TTL,Priority,Port,Weight | |
$CnameLookupResultObj | Add-Member NoteProperty "Value" $QueriedCnameRecordValue | |
$CnameLookupResultObj | Add-Member NoteProperty "Status" "OK" | |
## Only one record with single value can exists in CNAME, so you don't need to Enumerate the result & compare them ## | |
If ( $_.Value -eq $QueriedCnameRecordValue) | |
{ $_.FoundOnServer_2 = "Found"; $_.ValueOnServer_2 = $QueriedCnameRecordValue; $_.Status = "OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
elseif ($CnameLookupResultObj.Status -eq "OK" -AND $QueriedCnameRecordSection -eq "Answer" ) | |
{ $_.FoundOnServer_2 = "Not Matched"; $_.ValueOnServer_2 = $QueriedCnameRecordValue; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
else | |
{ $_.FoundOnServer_2 = "Not Found"; $_.ValueOnServer_2 = $QueriedCnameRecordValue; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
} | |
Catch { | |
$NotFound = 1; ## We cannot pass the $_ value to catch statement, so we have to set the flag | |
If ([string]$NotRespond -match "did not properly respond after a period of time" ) | |
{ | |
Write-Host -fore Red "Invalid Destination Server: $DestinationServer`nPlease make sure the Destination's DNS Service is up & running and there is no firewall rule blocked."; exit; | |
} | |
} | |
If ($NotFound) | |
{ $_.FoundOnServer_2 = "Not Found"; $_.ValueOnServer_2 =""; $_.Status = "NOT OK"; $_ | Select FQDN, RecordType, TTL, Priority, Port, Weight, TimeStamp, Value, FoundOnServer_2, ValueOnServer_2, Status } | |
} | |
#################------------------------------------------ CNAME compare section Ends Here --------------------------------##################### | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment