0

What's the API to get the top domain (root domain) from any domain in the hierarchy like tree or child domain?

The closest I could find were GetForest or GetCurrentForest but that's not what I am looking for..

[System.DirectoryServices.ActiveDirectory.Forest]::GetForest(directorycontext)

[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()

Thanks in advance, -SunMan

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
SunMan
  • 214
  • 1
  • 12
  • 1
    "[...] that's not what I am looking for" - can you elaborate a bit? How does the information returned by `GetCurrentForest()` not satisfy your requirement? – Mathias R. Jessen May 01 '18 at 13:40
  • sure.. Script is run by a user of forest A to find forest B. GetCurrentForest() gets a Forest object for the current user context ( the one who is logged in) and thus discovers Forest A everytime. – SunMan May 01 '18 at 14:35

1 Answers1

0

To get from a foreign domain name to its forest root domain, use Domain.GetDomain() first, then grab the root domain object through the Forest property:

$ForeignDomainDNS = 'other.domain.tld'
$ForeignDomainCtx = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext 'Domain',$ForeignDomain
$ForeignDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($ForeignDomainCtx)
$ForeignRootDomain = $ForeignDomain.Forest.RootDomain

$ForeignRootDomain now contains the Domain object representing the root domain of the foreign domain's forest

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206