top of page

PowerShell - Get all AD Site and Subnet Info

  • Writer: Jon Boyette
    Jon Boyette
  • Sep 27, 2022
  • 1 min read

Save this as a .ps1, Outputs to screen all Sites in the forest and all Subnets each site has in it.


$siteDescription=@{}

$siteSubnets=@{}

$subnetDescription=@{}

$sitesDN="LDAP://CN=Sites," + $([adsi] "LDAP://RootDSE").Get("ConfigurationNamingContext")

$subnetsDN="LDAP://CN=Subnets,CN=Sites," + $([adsi] "LDAP://RootDSE").Get("ConfigurationNamingContext")

#get the site names and descriptions

foreach ($site in $([adsi] $sitesDN).psbase.children){

if($site.objectClass -eq "site"){

$siteName=([string]$site.cn).toUpper()

$siteDescription[$siteName]=$site.description[0]

$siteSubnets[$siteName]=@()

}

}

#get the subnets and associate them with the sites

foreach ($subnet in $([adsi] $subnetsDN).psbase.children){

$subnetDescription[[string]$subnet.cn]=$subnet.description[0]

$site=[adsi] "LDAP://$($subnet.siteObject)"

if($site.cn -ne $null){

$siteName=([string]$site.cn).toUpper()

$siteSubnets[$siteName] += $subnet.cn

}else{

$siteDescription["Orphaned"]="Subnets not associated with any site"

if($siteSubnets["Orphaned"] -eq $null){ $siteSubnets["Orphaned"] = @() }

$siteSubnets["Orphaned"] += $subnet.cn

}

}

#write output to screen

foreach ($siteName in $siteDescription.keys | sort){

"$siteName $($siteDescription[$siteName])"

foreach ($subnet in $siteSubnets[$siteName]){

"`t$subnet $($subnetDescription[$subnet])"

}

}


 
 
 

Recent Posts

See All
PowerShell - List All Domain SPNs

Save as same List_ALL_SPNs.ps1 or similar, this LDap calls the Domain for all Service Principal names and accounts related #Build LDAP...

 
 
 
PowerShell - Start-Monitoring

This is a great script used to Monitor and Email if a server is up or down, once ran, and smtp and from address is set, then run:...

 
 
 

Comments


Post: Blog2 Post
  • Facebook
  • Twitter
  • LinkedIn

©2022 by Boyette Technical Services. Proudly created with Wix.com

bottom of page