Windows 2008 R2 does not have certain PowerShell cmdlets for pulling Active Directory information. A notable lack of PowerShell cmdlets for Sites and Site Links can make it harder to gather information and provide relevant data for either documentation, health checks or troubleshooting replication. So what can we do to get around this?
In Windows 2012R2 and greater we use cmdlets like these:
Get-ADReplicationSiteLink Get-ADReplicationSite
In 2008 R2, we see this:
Workaround Missing PowerShell Cmdlets
Well, our workaround dives a bit into .NET…. Before you run away now, I am not an expert in .NET, but I felt my way around like I would do in PowerShell. Trying to parse out the data that these commands provide us about Active Directory. When I was researching this I stumbled upon the .NET reference to pulling AD Forest information with this:
[System.DirectoryServices.ActiveDirectory.Forest]
What does that provide us? Not a lot.
Even adding a ‘| FL’ only adds A LOT more:
Now, from the same reference site, I found this method of pulling the information from here:
[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites
This then provides us information on each AD site. But its more in the format of a list instead of a table. So, ating like it’s PowerShell, we can add a ‘FT’ at th eend and get this:
We can tweak this a bit more if we would like by selecting certain objects. These values are available to us:
- Name
- Domains
- Subnets
- Servers
- AdjacentSites
- SiteLinks
- InterSiteTopologyGenerator
- Options
- Location
- BridgeheadServers
- PreferredSmtpBridgeheadServers
- PreferredRpcBridgeheadServers
- IntraSiteReplicationSchedule
For my reports, I use the above list to pull only these attributes:
[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites | ft Name,Intersite*,Subnets -Auto
This then leads to an attempt to get more out of the data:
Bad Attempt
([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites).SiteLinks | ft Name,Sites,Cost,ReplicationInterval
Why is this bad? Well, when you gather data with this cmdlet with the Format-Table at the end, it restricts what you can do with the data. Here is a real example:
Raw Data:
If we look at the data closely, we’ll see there are duplicates in the ‘Sites’ field in the middle. This is because we are querying both sides of a site link and thus pull the same site link twice. If we try to reduce this by using something like Sort-Object, we get an error:
If we read the error message, we see it mentions that the issue is cause by the ‘Format-*’ we used earlier. So let’s try it again:
Good Example
$SiteLinks = ([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites).SiteLinks | select-object Name,Sites,Cost,ReplicationInterval
Prove the data is the same AND sort the data by that column:
$SiteLinks | Sort-Object -Property Sites
Now that it’s sorted, we can now remove duplicate values with the -Unique switch:
$SiteLinks | Sort-Object -Property Sites -Unique
Now we have a good list of the site links, with the sites in the links as well as cost and replication intervals.