Search code examples
powershelldnsnslookup

Powershell NSlookup on DC's Forward and Reverse


$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)


Solution

  • $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 :)