Created a script this evening to bulk look up DNS information. By using the cmdlet Resolve-DnsName feeded with a list of domains from a text file, and using the ConvertTo-AceEncoding function from a previous post to be able to resolve IDN (International Domain Name) domains. This ended up with the script below, let’s take a look.
To start with I created an advanced function with two parameters. The
parameter lets you specify what kind of DNS records you want to resolve, where I specified A records as default value. The $Type
parameter is the path to where the list of domains is stored, I specified that the default list is stored in the C:\temp folder, since it’s often a one time lookup and not critical that the list is removed.$Path
The next step, the process
block.
function Resolve-BulkDnsName { [CmdletBinding()] param ( # Type of DNS records [Parameter(Mandatory = $false)] [String] $Type = "A", # Array of domains to lookup [Parameter(Mandatory = $false)] [String] $Path = "C:\temp\DomainList.txt" )
In the process
block I’m using the
cmdlet to collect the content from the Get-Content
$File
parameter and store it in the variable $DomainList
.
Then I’m creating the function
, which is called later within the ConvertTo-AceEncoding
foreach
loop. The function converts IDN (International Domain Names) to ACE encoded format (Punycode). For instance the domain
is an IDN domain using Norwegian characters like “æøå”, the result from the function is that krøllalfa.no
gets converted into alfakrøll.no
.xn--krllalfa-64a.no
You will notice that I added
messages that is used to debug and view the each step when the script is running. Without the messages the script will become a lot smaller. I added a script without the messages in the end.Write-Verbose
Further on I loop through all the elements in the
and check if the domain name contains Norwegian special characters by calling the function $DomainList
, then stores the domains to the file ConvertTo-AceEncoding
, and for domains that doesn’t contains special characters gets appended to the same ConvertedDomainList.txt.ConvertedDomainList.txt
In the last section of the process
block I’m using
to collect all converted domains and storing the result in the variable Get-Content
$Domains
.
Then I’m looping through all domains in
and piping it to the $Domains
cmdlet, then I’m selecting the properties I needed, then formatting the list to be more readable with Resolve-DnsName
.Format-List
Finally, the
variable is just an variable to empty the $EmptyFile
list to get a clean start for each time the script runs.ConvertedDomainList.txt
process { $DomainList = Get-Content -Path $Path function ConvertTo-AceEncoding { Param( [string]$Domain ) $Idn = New-Object System.Globalization.IdnMapping $Idn.GetAscii("$Domain"a) } Write-Verbose "Looping through Domain List..." foreach ($Domain in $DomainList) { if ($Domain -match "[æøå]") { Write-Verbose "Converting $Domain domain to ACE encoding..." $EncodedDomains = ConvertTo-AceEncoding -Domain $Domain Write-Verbose "Domain $Domain converted to $EncodedDomains..." Write-Verbose "Adding $EncodedDomains to Output file..." $EncodedDomains | Out-File -FilePath "C:\temp\ConvertedDomainList.txt" -Append } else { Write-Verbose "Appending $Domain to Output file..." $Domain | Out-File -FilePath "C:\temp\ConvertedDomainList.txt" -Append } } Write-Verbose "Getting list of converted domains.." $Domains = Get-Content -Path "C:\temp\ConvertedDomainList.txt" Write-Verbose "Performing bulk look up of domains in the ConvertedDomainList.txt file..." $Domains | ForEach-Object { Resolve-DnsName -Name $_ -Type $Type } | Select-Object -Property Name, Type, IP4Address, NameHost | Format-List $EmptyFile = $Null | Out-File -FilePath "C:\temp\ConvertedDomainList.txt" Write-Verbose "Script ended.." } }
To run the script:
# Use . source like below . .\Resolve-BulkDnsName.ps1 # Then you can run the command with default parameters Resolve-BulkDnsName
Remember to create the file
and fill it with some domains. I used some domains from this GitHub repo https://github.com/opendns/public-domain-lists/blob/master/opendns-top-domains.txt to have something to test on.C:\tempDomainList.txt
The result after running the script with default values, Resolve-BulkDnsName
.
And, the result after running with the default values and with the
switch, -Verbose
Resolve-BulkDnsName -Verbose
I think that is about it, a little useful and fun project for bulk resolving domain names. Comes in handy when you need to look up the A, CNAME or nameserver records for each domain. Have I found out. Below is the full script, with
and without.Write-Verbose
With
.Write-Verbose
function Resolve-BulkDnsName { [CmdletBinding()] param ( # Type of DNS records [Parameter(Mandatory = $false)] [String] $Type = "A", # Array of domains to lookup [Parameter(Mandatory = $false)] [String] $Path = "C:\temp\DomainList.txt" ) process { $DomainList = Get-Content -Path $Path function ConvertTo-AceEncoding { Param( [string]$Domain ) $Idn = New-Object System.Globalization.IdnMapping $Idn.GetAscii("$Domain") } Write-Verbose "Looping through Domain List..." foreach ($Domain in $DomainList) { if ($Domain -match "[æøå]") { Write-Verbose "Converting $Domain domain to ACE encoding..." $EncodedDomains = ConvertTo-AceEncoding -Domain $Domain Write-Verbose "Domain $Domain converted to $EncodedDomains..." Write-Verbose "Adding $EncodedDomains to Output file..." $EncodedDomains | Out-File -FilePath "C:\temp\ConvertedDomainList.txt" -Append } else { Write-Verbose "Appending $Domain to Output file..." $Domain | Out-File -FilePath "C:\temp\ConvertedDomainList.txt" -Append } } Write-Verbose "Getting list of converted domains.." $Domains = Get-Content -Path "C:\temp\ConvertedDomainList.txt" Write-Verbose "Performing bulk look up of domains in the ConvertedDomainList.txt file..." $Domains | ForEach-Object { Resolve-DnsName -Name $_ -Type $Type } | Select-Object -Property Name, Type, IP4Address, NameHost | Format-List $EmptyFile = $Null | Out-File -FilePath "C:\temp\ConvertedDomainList.txt" Write-Verbose "Script ended.." } }
Without
.Write-Verbose
function Resolve-BulkDnsName { [CmdletBinding()] param ( # Type of DNS records [Parameter(Mandatory = $false)] [String] $Type = "A", # Array of domains to lookup [Parameter(Mandatory = $false)] [String] $Path = "C:\temp\DomainList.txt" ) process { $DomainList = Get-Content -Path $Path function ConvertTo-AceEncoding { Param( [string]$Domain ) $Idn = New-Object System.Globalization.IdnMapping $Idn.GetAscii("$Domain") } foreach ($Domain in $DomainList) { if ($Domain -match "[æøå]") { $EncodedDomains = ConvertTo-AceEncoding -Domain $Domain $EncodedDomains | Out-File -FilePath "C:\temp\ConvertedDomainList.txt" -Append } else { $Domain | Out-File -FilePath "C:\temp\ConvertedDomainList.txt" -Append } } $Domains = Get-Content -Path "C:\temp\ConvertedDomainList.txt" $Domains | ForEach-Object { Resolve-DnsName -Name $_ -Type $Type } | Select-Object -Property Name, Type, IP4Address, NameHost | Format-List $EmptyFile = $Null | Out-File -FilePath "C:\temp\ConvertedDomainList.txt" } }
Have a great Easter, enjoy!