Exporting the ResolveDNS to CSV ?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Exporting the ResolveDNS to CSV ?

Post by ITEngineer »

Resolve-DNS-Export.PS1
(1.39 KiB) Downloaded 165 times
Hi All,

Based on the built-in PowerShell command

Code: Select all

Resolve-DnsName -Name bing.com -Type NS -DnsOnl
I need some help in fixing or modifying my PowerShell script below to perform:

1. Get the public IP address of the domain names in the .TXT file (or .CSV)
2. Get the DNS server responsible for the domain in the input .TXT file
3. Export the list as .CSV like below:

DomainName, PublicIP, DNS Server (NameHost)
www.domain1.com, 71.13.123.22, NS1.domain1.com
www.domain1.com, 71.13.123.25, NS2.domain1.com
www.domain2.com, 201.52.234.1, NS1.domain2.com

Input .TXT file:
www.domain1.com
www.domain2.com
...

Your help would be greatly appreciated.

Thanks,
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Exporting the ResolveDNS to CSV ?

Post by jvierra »

I do not understand what you are trying to do. What is the following?

$addresses = get-content $InputFile
$reader = New-Object IO.StreamReader $InputFile
while ($reader.ReadLine() -ne $null) { $TotalRecords++ }


Why use the stream reader to read the same file you just read?

$addresses = get-content $InputFile
$TotalRecords = $addresses.Count


What does the attached file have to do with creating a CSV.

Note that "Write-Host" cannot be redirected to a file.

For a CSV you will have to create a custom object.

See: https://kevinmarquette.github.io/2016-1 ... tomobject/
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Exporting the ResolveDNS to CSV ?

Post by mxtrinidad »

Just an FYI

If you're looking for consistent DNS information then this won't work. The Resolve-DnsName cmdlet example you provided shows different results every time it's been executed.

To work the results, JVierra lead you in the right direction, creating a custom object. Create two psobject (one for DNS_PTR and the other for DNS_A) then create a custom object to join the results.

:)
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Exporting the ResolveDNS to CSV ?

Post by ITEngineer »

Yes, that does make sense guys.

I will try to create the object and repost here if I need some scripting help again.

Thanks,
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Exporting the ResolveDNS to CSV ?

Post by jvierra »

Is this what you are trying to ask?

Code: Select all

$fqdn = 'bing.com'
Resolve-DnsName -Name $fqdn -Type NS -DnsOnl|
    Where{$_.Address} |
    select @{n='DomainName';e={$fqdn}},@{n='PublicIP';e={$_.IpAddress}},@{n='DNSServer';e={$_.Name}}
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Exporting the ResolveDNS to CSV ?

Post by ITEngineer »

jvierra wrote: Wed May 30, 2018 1:43 am Is this what you are trying to ask?

Code: Select all

$fqdn = 'bing.com'
Resolve-DnsName -Name $fqdn -Type NS -DnsOnl|
    Where{$_.Address} |
    select @{n='DomainName';e={$fqdn}},@{n='PublicIP';e={$_.IpAddress}},@{n='DNSServer';e={$_.Name}}
Yes, exactly, so the input will be the CSV file containing 55 domains.
/* IT Engineer */
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked