Played around with PowerShell the in the spare time on our yearly business trip. This year we went to Tallinn in Estonia. I wanted to automate look up changes for DNS, instead of typing commands like nslookup
or Resolve-DnsName
now and then, to see if the DNS cache got updated.
This ended with this script. Let’s break it down.
The script is built around the command Resolve-DnsName
, and this takes to parameters -Name
and -Type
, so I created the $Domain
and $Type
parameters.
param(
[string]$Domain,
[string]$Type
);
Further on, I created two variables containing each file path, and then I run the Resolve-DnsName
command to resolve current DNS information, then using the cmdlet Select-Object
selects the desired properties, then output this to $FileToCompareStaticPath
.
$FileToCompareStaticPath = "C:tempFileComparisonStatic.txt"
$FileToCompareDynamicPath = "C:tempFileForComparisonDynamic.txt"
Resolve-DnsName -name $Domain -Type $Type | Select Name, Type, IPAddress | Out-File $FileToCompareStaticPath
Moreover, now we come to the last part. Here I’m creating a loop that runs every 5 second, until there is difference in the file variables. For each round, I run the same command as before the loop, to create a dynamic updated file that gets compared with the static file created outside the loop. When the file is different it outputs a message and the content in each file, then the loop breaks.
While ($true)
{
Resolve-DnsName -Name $Domain -Type $Type | Select Name, Type, IPAddress | Out-File$FileToCompareDynamicPath
if (Compare-Object -ReferenceObject $(Get-Content $FileToCompareStaticPath) -DifferenceObject (Get-Content $FileToCompareDynamicPath))
{
Get-Content $FileToCompareDynamicPath
Write-Host -ForegroundColor Green "Yay, there has been changes to the file"
Write-Host "---------------------------------------------------------" ##
Write-Host "Static File, before changes." ##
Get-Content $FileToCompareStaticPath ## Formats outputof the changed files
Write-Host "Dynamic File, after changes" ##
Get-Content $FileToCompareDynamicPath ##
Write-Host "---------------------------------------------------------" ##
break
}else
{
Write-Host -ForegroundColor Yellow "No changes yet.."
Get-Content $FileToCompareStaticPath
}
Start-Sleep -Seconds 5
}
Here the full version of the script. There’s lots of improvements potential with input checks and validation, refactoring and so fort. Also when I tested the script on three different domains, they kept updating same files then broke each others loops. Thus a variable for file path is now implemented in the next section.
param(
[string]$Domain,
[string]$Type
);
$FileToCompareStaticPath = "C:tempFileComparisonStatic.txt"
$FileToCompareDynamicPath = "C:tempFileForComparisonDynamic.txt"
Resolve-DnsName -Name $Domain -Type $Type | Select Name, Type, IPAddress | Out-File $FileToCompareStaticPath
While ($true)
{
Resolve-DnsName -Name $Domain -Type $Type | Select Name, Type, IPAddress | Out-File $FileToCompareDynamicPath
if (Compare-Object -ReferenceObject $(Get-Content $FileToCompareStaticPath) -DifferenceObject (Get-Content $FileToCompareDynamicPath))
{
Get-Content $FileToCompareDynamicPath
Write-Host -ForegroundColor Green "Yay, there has been changes to the file"
Write-Host "---------------------------------------------------------" ##
Write-Host "Static File, before changes." ##
Get-Content $FileToCompareStaticPath ## Formats Output of the changes
Write-Host "Dynamic File, after changes" ##
Get-Content $FileToCompareDynamicPath ##
Write-Host "---------------------------------------------------------" ##
break
} else
{
Write-Host -ForegroundColor Yellow "No changes yet.."
Get-Content $FileToCompareStaticPath
}
Start-Sleep -Seconds 5
}
Here I added variables to store file names, so you can specify the desired file name and avoid overwriting the same files if you’re running the script on multiple domain.
# EXAMPLE
# Resolve-DnsNameChanges.ps1 -domain example.com -type A -staticFile exampleStatic exampledynamic
param(
[string]$Domain,
[string]$Type,
[string]$StaticFile,
[string]$DynamicFile
);
$fileToCompareStaticPath = "C:temp$StaticFile.txt"
$fileToCompareDynamicPath = "C:temp$DynamicFile.txt"
Resolve-DnsName -Name $Domain -Type $Type | Select Name, Type, IPAddress | Out-File $FileToCompareStaticPath
While ($true)
{
Resolve-DnsName -Name $Domain -Type $Type | Select Name, Type, IPAddress | Out-File $FileToCompareDynamicPath
if (Compare-Object -ReferenceObject $(Get-Content $FileToCompareStaticPath) -DifferenceObject (Get-Content $FileToCompareDynamicPath))
{
Get-Content $FileToCompareDynamicPath
Write-Host -ForegroundColor Green "Yay, there has been changes to the file"
Write-Host "---------------------------------------------------------" ##
Write-Host "Static File, before changes." ##
Get-Content $FileToCompareStaticPath ## Formats Output of the changes
Write-Host "Dynamic File, after changes" ##
Get-Content $FileToCompareDynamicPath ##
Write-Host "---------------------------------------------------------" ##
break
} else
{
Write-Host -ForegroundColor Yellow "No changes yet.."
Get-Content $FileToCompareStaticPath
}
Start-Sleep -Seconds 5
}
Once again a, I found some issues with the script above, so I added some new changes:
I removed resolving TTL from the script, since it breaks the script because each nameserver might have different TTL and causes the script to break before the actually DNS records gets updated. Also, I added the possibility to resolve more than A records, as the script above does. Now you can query for ANY, ALL, CNAME, TXT, A, AAAA, MX and other records. Also I refactored the script a bit with help comments, and by making it an Advanced PowerShell Function. So, that later on, the script can become a part of a Toolset module with my own created scripts. So now the script looks like the following:
Note: To run the script you can use dot source . .Resolve-DnsNameChanges.ps1
to load the script into memory, then call the function with Resolve-DnsNameChanges
followed by the parameters, for instance:
# Load script into memory
. .Resolve-DnsNameChanges.ps1
# Then call the function with parameters
# Note: The ´ character allow you to write PowerShell scripts in multiple lines.
Resolve-DnsNameChanges -Domain fredrikengseth.com ´
-Type A -StaticFile static-fredrikengseth.com ´
-DynamicFile dynamic-fredrikengseth.com
<#.SYNOPSIS
The script looks up changes to DNS
.DESCRIPTION
The script looks up changes to DNS, to see if the DNS cache is updated.
Useful, when you need to connect to a service like Office365 and you need to wait for
the DNS to be updatet to continue configuration for the service.
.EXAMPLE
PS C:> Resolve-DnsNameChanges -Domain fredrikengseth.com -Type A -StaticFilestatic-fredrikengseth.com -DynamicFile dynamic-fredrikengseth.com
The Command above looks up DNS changes to the DNS A-record on fredrikengseth.com.
The Staticfile stores the current DNS cache, and the DynamicFile resolves for new DNS updateevery 5 seconds and
compare it to the static file.
.NOTES
The TTL is removed from the script, since it breaks the script because each nameserver mighthave different TTL, and
it isn't what I want to find. But instead changes to A, CNAME, TXT, MX and so forth.
#>
function Resolve-DnsNameChanges {
[CmdletBinding()]
param(
[string]$Domain,
[string]$Type,
[string]$StaticFile,
[string]$DynamicFile
);
process {
$fileToCompareStaticPath = "C:temp$StaticFile.txt"
$fileToCompareDynamicPath = "C:temp$DynamicFile.txt"
Resolve-DnsName -Name $Domain -Type $Type | Select-Object Name, Type, Section, IPAddress,NameHost | Out-File $FileToCompareStaticPath
While ($true) {
Resolve-DnsName -Name $Domain -Type $Type | Select-Object Name, Type, Section,IPAddress, NameHost | Out-File $FileToCompareDynamicPath
if (Compare-Object -ReferenceObject $(Get-Content $FileToCompareStaticPath -DifferenceObject $(Get-Content $FileToCompareDynamicPath)) {
Get-Content $FileToCompareDynamicPath
Write-Host -ForegroundColor Green "Yay, there has been changes to the file"
Write-Host "---------------------------------------------------------" ##
Write-Host "Static File, before changes." ##
Get-Content $FileToCompareStaticPath ## Thisformats the output of the changed files
Write-Host "Dynamic File, after changes" ##
Get-Content $FileToCompareDynamicPath ##
Write-Host "---------------------------------------------------------" ##
break
}
else {
Write-Host -ForegroundColor Yellow "No changes yet.."
Get-Content $fileToCompareStaticPath
}
Start-Sleep -Seconds 5
}
}
}
All in all, this was a fun little project, which seems to be in constant improvement, as a notice error and desired needs, and improvements. Thought I should share it just for fun.
Have a great day!