Using PowerShell to bulk look up DNS for domain names


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 $Type parameter lets you specify what kind of DNS records you want to resolve, where I specified A records as default value. The $Path 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.

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 Get-Content cmdlet to collect the content from the $File parameter and store it in the variable $DomainList.

Then I’m creating the function ConvertTo-AceEncoding, which is called later within the foreach loop. The function converts IDN (International Domain Names) to ACE encoded format (Punycode). For instance the domain krøllalfa.no is an IDN domain using Norwegian characters like “æøå”, the result from the function is that alfakrøll.no gets converted into xn--krllalfa-64a.no.

You will notice that I added Write-Verbose 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.

Further on I loop through all the elements in the $DomainList and check if the domain name contains Norwegian special characters by calling the function ConvertTo-AceEncoding, then stores the domains to the file ConvertedDomainList.txt, and for domains that doesn’t contains special characters gets appended to the same ConvertedDomainList.txt.

In the last section of the process block I’m using Get-Content to collect all converted domains and storing the result in the variable $Domains.

Then I’m looping through all domains in $Domains and piping it to the Resolve-DnsName cmdlet, then I’m selecting the properties I needed, then formatting the list to be more readable with Format-List.

Finally, the $EmptyFile variable is just an variable to empty the ConvertedDomainList.txt list to get a clean start for each time the script runs.

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 C:\tempDomainList.txt 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.

The result after running the script with default values, Resolve-BulkDnsName.

Resolve-BulkDnsName result without verbose switch

And, the result after running with the default values and with the -Verbose switch, Resolve-BulkDnsName -Verbose

Resolve-BulkDnsName result with verbose switch

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 Write-Verbose and without.

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!


For More Content See the Latest Posts