Powershell DNS - how do i search a list of DNS Servers and then filter the results?

2

I am trying to search a list of specific DNS servers (i have in a file) and then query for a specific host name. I can do this bit :)

The next bit is I wish to return a list of those DNS Servers which return a result which other than outlook-emea*, I want the IP of the DNS Server and also the result.

The problem i have is the DNS command returns CNAMES and A records - i am only interested in A records and also i am not sure how to filter the results. This is what I have so far.

$Address = 'outlook.office365.com'

#$listofIPs = Get-Content 'C:\Users\user1\file.txt'

$listofIPs = '8.8.8.8'

$ResultList = @()

foreach ($ip in $listofIPs)

{

 $Result = Resolve-DnsName -Name $Address -Type A -Server $ip

Write-Host ""
Write-Host DNS Server: -foregroundcolor "green" $ip 
Write-Host ""
Write-Host Resolved Names: -foregroundcolor "green"

}

Can someone help?

Dave Davidson

Posted 2015-06-04T14:46:51.697

Reputation: 23

Does my answer suits your needs ? If so, please up vote it and mark it as accepted answer. If it doesn't, please explain how I can improve it. Thanks – Ob1lan – 2015-06-06T18:57:29.263

Thank you ever so much!!! I will now try understand the code to learn from it. many thanks . Im sorry my rep is too low to UpVote....but it is perfect! – Dave Davidson – 2015-06-08T09:42:51.577

Answers

0

Here is the script I have so far, based on your's :

$Address = "outlook.office365.com"

$listofIPs = Get-Content "C:\file.txt"

$ResultList = @()

foreach ($ip in $listofIPs)

{
    # The following query will list only records begining with "outlook-", but not begining with "outlook-emea"
    $DNSquery = (Resolve-DnsName -Name $Address -Type A -Server $ip).Name | Where-Object {$_ -inotlike "outlook-emea*" -and $_ -ilike "outlook-*"}

    # We assume, based on several tests, that selecting the first result for the previous query is enough.
    $Result = $DNSquery | Select -First 1

    if ($DNSquery)
    {
        # Creating custom object to feed the array
        $Object = New-Object PSObject
        $Object | Add-Member -MemberType NoteProperty -Name "DNS Server IP" -Value $ip
        $Object | Add-Member -MemberType NoteProperty -Name "Result" -Value $Result
        $ResultList += $Object
    }

    # Displaying the array with the results
    $ResultList
}

And here is the result I have, when my text file contains 8.8.8.8, 8.8.8.4, 173.255.0.194 and 173.201.20.134 :

DNS query result

Ob1lan

Posted 2015-06-04T14:46:51.697

Reputation: 1 596

I edited out the portion where you've asked the OP to accept your answer. If you want you can add it as a comment directed at people new to StackExchange, but it shouldn't be part of your answer itself. – Karan – 2015-06-05T20:45:38.407