Share This Post

PowerShell to identify the GlobalCatalog Servers and more in your AD environment – Part II

Lets continue play with AD using PowerShell.

PS C:\Documents and Settings\yl.admin\My Documents\PS>$myforest = [System.DirectoryServices.AcitveDirectory.Forest]::GetCurrentForest()

PS C:\Documents and Settings\yl.admin\My Documents\PS> $myforest.Sites

Name : NYT
Domains : {mydomain.com}
Subnets : {xxx.yyy.0/16}
Servers : {dc1.mydomain.com, NYTDCP01.mydomain.com, NYTDCP02.mydomain.com, dc2.mydomain.com}
AdjacentSites : {COA, NYH, NJC, NYR…}
SiteLinks : {NYT-To-COP-10, NYT-To-NJC-10, NYT-To-NY5-10, NYT-To-NYB-10…}
InterSiteTopologyGenerator : NYTDCP02.mydomain.com
Options : None
Location : NY/TW
BridgeheadServers : {NYTDCP02.mydomain.com, NYTDCP01.mydomain.com, dc1.mydomain.com, dc2.mydomain.com}
PreferredSmtpBridgeheadServers : {}
PreferredRpcBridgeheadServers : {}
IntraSiteReplicationSchedule :

Name : NJC
Domains : {mydomain.com}
Subnets : {}
Servers : {njc.mydomain.com}
AdjacentSites : {NYH, NYT, CAL, CAG…}
SiteLinks : {NYT-To-NJC-10, NJC-To-CAL-100, NJC-To-CAG-100, NJC-To-CAI-100…}
InterSiteTopologyGenerator : njcdcp01.mydomain.com
Options : None
Location : New Jersey
BridgeheadServers : {njcdcp01.mydomain.com}
PreferredSmtpBridgeheadServers : {}
PreferredRpcBridgeheadServers : {}
IntraSiteReplicationSchedule :

.

.

Two things I don’t like about the output is:

1) The format

2) What about the information after … – we simply can’t see!

So I gathered enough strength and time to write a script to get the output into excel format which I like (My bosses like too!) and to enumerate through those arrays to get complete information:

$a = New-Object -comobject Excel.Application
$a.visible = $True

$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)

$c.Cells.Item(1,1) = "SiteName"
$c.Cells.Item(1,2) = "Domains"
$c.Cells.Item(1,3) = "Subnets"
$c.Cells.Item(1,4) = "Servers"
$c.Cells.Item(1,5) = "AdjacentSites"
$c.Cells.Item(1,6) = "SiteLinks"
$c.Cells.Item(1,7) = "ISTG"
$c.Cells.Item(1,8) = "Location"
$c.Cells.Item(1,9) = "BridgeheadServers"
$c.Cells.Item(1,10) = "Report Time Stamp"

$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True

$intRow = 2

$MyForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$Sites = MyForest.Sites
Foreach($Site in $Sites)
{
$c.Cells.Item($intRow,1) = $Site.Name
Foreach ($domain in $Site.domains)
{$Dom+=$domain.name + ";"
$c.Cells.Item($intRow,2) = $Dom}

Foreach($Subnet in $Site.Subnets)
{$Sub+=$Subnet.name + ";"
$c.Cells.Item($intRow,3) = $Sub}

Foreach($Server in $Site.Servers)
{$Ser+=$Server.name + ";"
$c.Cells.Item($intRow,4) = $Ser}

Foreach($adjacentSite in $site.adjacentSites)
{$adj+= $adjacentSite.name + ";"
$c.Cells.Item($intRow,5) = $Adj}

Foreach($SiteLink in $Site.SiteLinks)
{$Sit += $SiteLink.name + ";"
$c.Cells.Item($intRow,6) = $Sit}

$c.Cells.Item($intRow,7) = $Site.InterSiteTopologyGenerator.name
$c.Cells.Item($intRow,8) = $Site.Location

Foreach($BridgeHead in $Site.BridgeheadServers)
{$Bri += $BridgeHead.name + ";"
$c.Cells.Item($intRow,9) = $Bri}

$c.Cells.Item($intRow,10) = Get-date

$Dom = $null
$Sub = $null
$Ser = $null
$adj = $null
$Sit = $null
$Bri = $null
$intRow = $intRow + 1

}
$d.EntireColumn.AutoFit()

Share This Post

Ying Li obtained a Master degree of Medicine in China and worked in the medical field before he came to the states in 1996. He made the career switch from troubleshooting people to troubleshooting computers after he came to the states. Not entirely a surprise there, if you know that his second preference for college is Mathematics. Ying is two times Admin Framework MVP awardee (2007-2008).His previous employment experience in the US includes UUNET, MCI WorldCom. MMC (Marsh & McLennan Companies), Merrill Lynch. He is currently employed by Related Companies as a Senior Infrastructure Engineer. He is proficient in virtually all Microsoft products (Office/Back office). His craving for efficiency naturally leads him to scripting, automation and before you know it, PowerShell! He is currently a MCSE (NT4, 2000, 2003) and MCITP Server Administrator. Ying enjoys wines and love to travel. His non work related passion is to advocate Health Living!

Leave a Reply