Resolve DNS script for a list of subdomain names

Welcome Forums General PowerShell Q&A Resolve DNS script for a list of subdomain names

Viewing 4 reply threads
  • Author
    Posts
    • #197300
      Participant
      Topics: 1
      Replies: 2
      Points: 19
      Rank: Member

      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 topic was modified 1 week, 3 days ago by dawnifier83.
    • #197309
      Participant
      Topics: 10
      Replies: 1375
      Points: 1,481
      Helping Hand
      Rank: Community Hero

      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

    • #197459
      Participant
      Topics: 1
      Replies: 2
      Points: 19
      Rank: Member

      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!

    • #197486
      Participant
      Topics: 10
      Replies: 1375
      Points: 1,481
      Helping Hand
      Rank: Community Hero

      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
      
    • #197501
      Participant
      Topics: 1
      Replies: 2
      Points: 19
      Rank: Member

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

      PowerShell
      3 lines

      1
      2
      3
      msn.com
      microsoft.com
      azure.microsoft.com
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      Then you can do something like so:

      PowerShell
      8 lines

      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

      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.

Viewing 4 reply threads
  • You must be logged in to reply to this topic.