Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Checking against multiple domains

$
0
0

Hi all

The script below works perfectly fine, but I feel there is a better way to write it/condense it. We have a list of nearly 2,800 servers that we have to confirm which of the 8 domains they belong to. When I run this script from the root domain it checks against that domain and, if not a part of the root domain, then moves onto the first child domain, and then the next child domain, and so on.

-----------------------------------------------------------

$servers = Get-Content c:\scripts\servers.txt
foreach ($server in $servers){

  If (test-connection $server){

       write-host "Checking which domain $server is part of..."
       try {
            $a = Get-ADComputer -Identity $server
            Write-Host "$server is member of rootdomain"
           }

       catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {

             try {
                  $a = Get-ADComputer -Identity $server -server childdomain1
                  Write-Host "$server is member of childdomain1"
                 }

             catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {

                  try {
                       $a = Get-ADComputer -Identity $server -server childdomain2
                       Write-Host "$server is member of childdomain2"
                      }

                  catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{

                       try {
                            $a = Get-ADComputer -Identity $server -server childdomain3
                            Write-Host "$server is member of childdomain3"
                           }

                       catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{

                               Write-Host "$server not found on any domain"
                           }
                   }
             } 
       }

  }Else{
        Write-Host "$server not responding"
       }
}

-------------------------------------------------------

That's a lot of Try/Catch when you have 7 child domains, so I was wondering if there was a way to check against multiple domains in a better way.

~Rick


Viewing all articles
Browse latest Browse all 15028

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>