0
$topDC1="10.254.90.17"
$topDC2="10.225.224.17"
$topDC3="10.110.33.32"
$topDC4="10.88.100.10"
$DomainName="office.adroot.company.net"
TRY{    
$hostname = [System.Net.DNS]::GetHostByName($topDC1).HostName.toupper()
$ipaddress = [System.Net.Dns]::GetHostAddresses($DomainName) | select IPAddressToString -ExpandProperty IPAddressToString
# I want the below to loop foreach ip in the object, ns it against all 4 topDC's, then output each result :( 
$NS1 = nslookup $ipaddress[0] $topDC1
Write-host $NS1
}
Catch{
write-host "error"
}
Here is my dirty code so far (just to keep it simple)

I am trying to automate this: NSLOOKUP office.adroot.company.net put the results into an object for each ip in results, do an NSLOOKUP against our top level DC's. find which DC's haven't been cleaned up after decommission (still in dns)

Brent S
  • 83
  • 1
  • 11
  • What is your desired outcome? What information do you want returned by the code? You say you want to test each IP address with each DC. Is that all, or do you want the results processed in some way? Also, why test the addresses against all DCs in the first place? Your DNS zone should be AD-integrated (i.e. be present on all DCs), so querying one DC should suffice. – Ansgar Wiechers May 21 '15 at 18:10

2 Answers2

0

Try this:

$topDomainControllers = @("10.254.90.17", "10.225.224.17", "10.110.33.32", "10.88.100.10")

$DomainName="office.adroot.company.net"

try {
  $hostname = [System.Net.Dns]::GetHostByName($topDC1).HostName.ToUpper()
  $ipAddresses = [System.Net.Dns]::GetHostAddresses($DomainName) |
                 select -ExpandProperty IPAddressToString

  foreach ($ipAddress in $ipAddresses) {
    $nslookupResult = nslookup $ipAddress
    $foundIp = $nslookupResult[1] -match "^\D*(\d+\.\d+\.\d+\.\d+)$"

    if ($foundIp -eq $false) {
      continue
    }

    $domainController = $Matches[1]
    if ($topDomainControllers.Contains($domainController)) {
      Write-Output -Verbose "Found domain controller match for $domainController"
      break
    } else {
      Write-Output -Verbose "No match found for domain controller $domainController"
    }
  }
} catch {
  Write-Output "An error has occured: $_"
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
MonkeyDreamzzz
  • 3,978
  • 1
  • 39
  • 36
  • The second line of (Windows) `nslookup` output contains the IP address of the nameserver, not the resolved address. Also, the OP said he wanted lookups of each address against each nameserver. – Ansgar Wiechers May 21 '15 at 18:13
  • @AnsgarWiechers I thought the user wanted to get the nameserver. Was not very clear from his question. This code works en it seems that it helped the user find the answer himself so seems a bit harsh to downvote it. – MonkeyDreamzzz May 22 '15 at 07:53
  • If I had 15 rep I'd vote it up. It was very nice of you to make the attempt, and I am sure it will be useful for someone. I had a very difficult time wording the question, so it's my bad :) – Brent S May 22 '15 at 12:13
0
$DCList="10.254.90.17","10.225.224.17","10.110.33.32","10.88.100.10"
$DomainName="office.adroot.blorg.net","pcd.blorg.ca","blorg.ca","percom.adroot.blorg.net", "blorg.blorg.net","ibg.blorg.net","sacmcm.adroot.blorg.net","sysdev.adroot.blorg.net","adroot.blorg.net"
TRY{    
    foreach ($DomainNameItem in $DomainName){
        Write-Host ""
        Write-Host ""
        Write-Host "Looking UP result"$DomainNameItem -foreground yellow
        Write-Host ""
        $hostname = [System.Net.DNS]::GetHostByName($DCListItem).HostName.toupper()
        $ipaddress = [System.Net.Dns]::GetHostAddresses($DomainNameItem).IPAddressToString
            foreach ($ip in $ipaddress){
                Write-Host ""
                Write-Host "Looking UP result"$ip -foreground green
                    foreach ($topdns in $DCList){
                        $RESULTS = nslookup $ip $topdns
                        Write-host $RESULTS
                }        
            }
    }
}
Catch{
write-host "error"
}
Write-Host ""
Write-Host ""
pause

Got it! This will save me tonnes of work determining if a DNS cleanup is necessary. Thanks guys, I'm learning just how great Powershell can be :)

Brent S
  • 83
  • 1
  • 11
  • 2
    Great that you found the answer yourself. In your case it is better to use `Write-Output` instead of `Write-Host`. 'Write-Host` should only be used in very specific cases. See [here](http://stackoverflow.com/questions/8755497/which-should-i-use-write-host-write-output-or-consolewriteline) for more info. – MonkeyDreamzzz May 22 '15 at 07:59