PowerShell - List all Subnets with Site Name in Forest
- Jon Boyette
- Jan 10, 2022
- 1 min read
Save as a .ps1. run and exports all Subnets in Forest and Site Names to user profile folder as AD-Subnets.csv
## Get a list of all domain controllers in the forest
$DcList = (Get-ADForest).Domains | ForEach { Get-ADDomainController -Discover -DomainName $_ } | ForEach { Get-ADDomainController -Server $_.Name -filter * } | Select Site, Name, Domain
## Get all replication subnets from Sites & Services
$Subnets = Get-ADReplicationSubnet -filter * -Properties * | Select Name, Site, Location, Description
## Create an empty array to build the subnet list
$ResultsArray = @()
## Loop through all subnets and build the list
ForEach ($Subnet in $Subnets) {
$SiteName = ""
If ($Subnet.Site -ne $null) { $SiteName = $Subnet.Site.Split(',')[0].Trim('CN=') }
$DcInSite = $False
If ($DcList.Site -Contains $SiteName) { $DcInSite = $True }
$RA = New-Object PSObject
$RA | Add-Member -type NoteProperty -name "Subnet" -Value $Subnet.Name
$RA | Add-Member -type NoteProperty -name "SiteName" -Value $SiteName
$RA | Add-Member -type NoteProperty -name "DcInSite" -Value $DcInSite
$RA | Add-Member -type NoteProperty -name "SiteLoc" -Value $Subnet.Location
$RA | Add-Member -type NoteProperty -name "SiteDesc" -Value $Subnet.Description
$ResultsArray += $RA
}
## Export the array as a CSV file
$ResultsArray | Sort Subnet | Export-Csv .\AD-Subnets.csv -nti
Comments