I have a big forest with many AD Domain, and frequently add new domains. I need a list of these Ad Domain to query each DC.
2 Answers
GetAllTrustRelationships()
is not a very failsafe way to enumerate all domains in the forest, and may include other forests/domains as well.
Grab the Domains
references from the Forest object instead:
function Get-ADInside
{
$Forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$Domains = $Forest.Domains
$Domains |Select -ExpandProperty Name
}
-
this code only include the current domain, not other in the current forest.– AlbanJan 11, 2016 at 15:31
this function list your Active Directory Domain avaible in the forest (include the current domain => $Forest.Domains.name)
function get-AdInside () {
$MainForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$ForestTrusts = $MainForest.GetAllTrustRelationships()
$ADs = @($MainForest.Domains.name)
$ADs += $ForestTrusts.TargetName
return $ADs
}
tested with current domain 2008-2008r2-2012+
-
1