When writing scripts I have always been a fan of making them as generic as possible. This may make the script a tad more complicated, but it allows it to be dynamic and also allows you to share these scripts between environments (i.e. Lab, QC, Production.) Basically we want to avoid hardcoding Domains, Domain Controllers, OUs, Containers, and site info.
Below I provide some simple examples of getting this information dynamically. This will allow you to discover the information instead of hardcoding it in the script.
To get forest information like Domains, Sites, ForestMode, RootDomain, and Forest masters you can use this:
$Forest = [DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
To get Domain information like Domain Controllers, DomainMode, Domain Masters, and Forest Root.
$Domain = [DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
To get the current Site information for the local machine like Subnets, Sitelinks, Location, Bridgehead Servers, and Domain Controllers.
$MySite = [DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite()
With these variables you can find all the Active Directory infrastructure information you could possibly want.
Here are some more specific examples on how to use these variables:
To find all your Global Catalogs in the forest
$Forest.GlobalCatalogs
To find all the Domain Controllers in the current domain
$Domain.DomainControllers
To see what application partitions your forest has
$forest.ApplicationPartitions
To see the forest roles
$forest | select SchemaRoleOwner,NamingRoleOwner
To see the domain roles
$domain | select PDCRoleOwner,RidRoleOwner,InfrastructureRoleOwner
To see the subnets in the current site
$MySite.subnets
To see the bridgehead Servers
$MySite.BridgeheadServers
tshell :: Jun.14.2009 ::
Active Directory, All ::
No Comments »