Forum Discussion
robmo
Feb 17, 2023Brass Contributor
Checking if a computer is in the the Active Directory global catalog
Hi, I need a script that can tell me if a computer is found in the Active Directory global catalog. The code below is returning inaccurate results. Every computer that failed the Try statement can b...
- Feb 17, 2023
I added some bogus computer names to computers.txt along with some known computers and it seems to be working now. Let me know if this solution doesn't seem correct.
Import-Module ActiveDirectory $server = "myserver.com:3268" #Get the list of computers to test $computers = Get-Content -Path "$PSScriptRoot\computers.txt" #Check active directory for each computer foreach($computer in $computers) { $test = Get-ADComputer -Filter 'Name -eq $computer' -Server $server if($null -ne $test) { Write-Host "$computer in AD" -ForegroundColor Green } else { Write-Host "$computer not in AD" -ForegroundColor Red } }
robmo
Feb 17, 2023Brass Contributor
Changing Line 11 to the example below appears to show the correct results. Would this change be reliable?
$test = Get-ADComputer -Filter 'Name -eq $computer'robmo
Feb 17, 2023Brass Contributor
I added some bogus computer names to computers.txt along with some known computers and it seems to be working now. Let me know if this solution doesn't seem correct.
Import-Module ActiveDirectory
$server = "myserver.com:3268"
#Get the list of computers to test
$computers = Get-Content -Path "$PSScriptRoot\computers.txt"
#Check active directory for each computer
foreach($computer in $computers) {
$test = Get-ADComputer -Filter 'Name -eq $computer' -Server $server
if($null -ne $test) {
Write-Host "$computer in AD" -ForegroundColor Green
} else {
Write-Host "$computer not in AD" -ForegroundColor Red
}
}