Resolve DNS script for a list of subdomain names

Hi All,

It’s been a while since I used Powershell and so I am in a rush in relearning it for some projects. For one of them, I need to do a resolve-dns lookup for a list of subdomains.

Here is one of the approaches I went with:

$domains = Get-Content @(C:\Users\myself\Subdomains.txt)

$domains | Resolve-DnsName -Type ANY -Server 8.8.8.8

Any idea why this wouldn’t work and how I can fix this issue? I tried using a loop to go through the list but I kept on ending up with no errors but no files or output being generated.

This basic example worked for me:

$domains = 'www.msn.com','www.microsoft.com','azure.microsoft.com'

$domains | Resolve-DnsName -Type ANY -Server 8.8.8.8

Output:

Name                           Type   TTL   Section    NameHost                                                                                                                                                                                                  
----                           ----   ---   -------    --------                                                                                                                                                                                                  
www.msn.com                    CNAME  173   Answer     www-msn-com.a-0003.a-msedge.net                                                                                                                                                                           
www.microsoft.com              CNAME  2832  Answer     www.microsoft.com-c-3.edgekey.net                                                                                                                                                                         
azure.microsoft.com            CNAME  3260  Answer     acom.trafficmanager.net  

There isn’t a reason to wrap the file name with @(), basically that is passing multiple paths to Get-Content. If the file is formatted like:

Server1
Server2
Server3

then it will be an array when import with Get-Content

Hmm I see, thank you for the response. By chance, do you have a script that resolves the DNS name of a list of subdomains? I ask because the original reason why I tried to use the get-content from a file is due to the fact that I’m looking up a decent sized list of subdomains that occasionally change. I will look into arrays based off the second half of your response.

Again, I appreciate the response!

Based on the second half, you can use Get-Content. If your txt file looked like so:

msn.com
microsoft.com
azure.microsoft.com

Then you can do something like so:

$domains = Get-Content C:\Scripts\domains.txt

$results = foreach ($domain in $domains) {
    Resolve-DnsName -Name $domain  -Type ANY -Server 8.8.8.8
}

$results
This should only return valid subdomains. Just download a list of common subdomains and pass that in on the first line and change the domain.
$subdomain_list = Get-Content -Path "C:\Some\file\with\subdomains.txt"
$domain = "example.com"

foreach($subdomain in $subdomain_list) {
try {
# We only care if it resovles so discard results
$null = Resolve-DnsName -Name “$subdomain.$domain” -ErrorAction Stop
# Will only output valid subdomains
Write-Output “$subdomain.$domain:VALID”
} catch {}
}

[quote quote=197486]Based on the second half, you can use Get-Content. If your txt file looked like so:

PowerShell
3 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
msn.com
microsoft.com
azure.microsoft.com
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Then you can do something like so:
PowerShell
8 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
6
7
8
$domains = Get-Content C:\Scripts\domains.txt
$results = foreach ($domain in $domains) {
Resolve-DnsName -Name $domain -Type ANY -Server 8.8.8.8
}
$results
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]

I’ve been getting this error:

Resolve-DnsName : xyz.example.com : DNS name does not exist At line:3 char:5 + Resolve-DnsName -Name $domains -Type ANY -Server 8.8.8.8 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (xyz.example.com:String) [Resolve-DnsName], Win32Exception + FullyQualifiedErrorId : DNS_ERROR_RCODE_NAME_ERROR,Microsoft.DnsClient.Commands.ResolveDnsName

This error shows up for each subdomain on the list.

 

*replaced my subdomains with xyz.example.com to comply with this site’s rules.