7,023 questions
#
Define the target OU paths
$ouPaths = @(
"domain1\bu\bu_name\computers",
"domain2\bu\bu_name\computers",
"domain3\bu\bu_name\computers"
)
# Iterate over each domain
foreach ($ouPath in $ouPaths) {
# Extract the domain name from the OU path
$domain = $ouPath.Split("\")[0]
# Connect to the domain
$domainContext = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext("Domain", $domain)
$domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($domainContext)
$domainDN = $domain.GetDirectoryEntry().distinguishedName
# Perform a recursive search within the target OU
$searcher = New-Object System.DirectoryServices.DirectorySearcher
$searcher.SearchRoot = "LDAP://$domainDN"
$searcher.Filter = "(&(objectClass=computer)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))"
$searcher.SearchScope = [System.DirectoryServices.SearchScope]::Subtree
# Retrieve and display the active computer names
$results = $searcher.FindAll()
foreach ($result in $results) {
$computer = $result.Properties["name"]
Write-Host "Active computer found in $ouPath: $computer"
}
# Clean up the searcher object
$searcher.Dispose()
}
Hope this helps!!