Forum Discussion
JimLeary
Jan 14, 2020Copper Contributor
Change The Domain To Search With Powershell
How can I change the domain I query with the Active Directory Module? We have 5 unique root domains. I use Get-ADuser, Get-ADcomputer, Get-ADObject, Get-ADReplicationSubnet, but I can only search wit...
PeterJ_Inobits
Jan 16, 2020Iron Contributor
Hi Jim
I've used this approach in the past assuming that all the domains are in the same forest
Use get-adforest to retrieve list of domains.
For each domain use get-addomain to retrieve list of DC's or the PDCEmulator role holder.
feed that name into the cmdlet using the -server switch..
I've got a working code sample that I can dig out and send to you if you would like
- JimLearyJan 17, 2020Copper Contributor
PeterJ_Inobits, that would be great if finding the search isn't any trouble. I put together something similar. After I realized the Find Role, I had to export the DC's. Here's mine.
$AsburyDomain="asbury.localhost"
$context = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext("domain",$AsburyDomain)
[system.directoryservices.activedirectory.domain]::GetDomain($context).domainControllers | export-csv "c:\users\Desktop\Domain_Controller_List\domain.csv" -NoTypeInformation -Encoding UTF8- PeterJ_InobitsJan 17, 2020Iron Contributor
Hi
So you are trying do dump the dc information out for each domain in the forest right?
Assuming you have the AD module installed and are on a domain joined machine then the code looks something like this:
import-module ActiveDirectory -force
$adforest=get-adforest
$domainlist=$adforest.domains
foreach($domain in $domainlist)
{
$pdc=(Get-ADDomain -identity $domain).pdcemulator
$dclist=(get-addomain -identity $domain -server $pdc ).replicadirectoryservers
foreach($dc in $dclist)
{
get-addomaincontroller -identity $dc | export-csv -notypeinformation -path dclist.csv -append
}
}
Hope this helps...
- JimLearyJan 17, 2020Copper Contributor
PeterJ_Inobits It does. thx.