The script below was written so the networking staff would be able to always have a current list of the AD Sites and Subnets, without relying on the Active Directory Module.
<# Get-ADSubnets.ps1 Alan Kaplan 1/24/20 Get list of AD Subnets in Forest Does not rely on AD module or admin rights #> #Default logfile $desktop = [environment]::GetFolderPath('Desktop') $logfile = "$Desktop\SitesAndSubnets_" + $(Get-Date).ToString("yyyyMMdd_HHmm") + '.csv' $Forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest() Write-Host "Getting All Sites and Subnets in $forest, please wait ...." @( $forest.Sites | Where-Object { $_.domains.count -eq 1 } | Foreach-Object { Write-Progress $_.Name [PScustomObject]@{ Domain = $_.Domains[0] Site = $_.name Subnets = $_.Subnets -join "; " DCs = $_.Servers -join "; " Location = $_.location -replace '\,',',' } }) | Sort-Object Domain, Site | Export-csv $logfile -NoTypeInformation Write-Progress "Done" -completed Clear-Host Write-host "Done. Report is $logfile" #Open report when done #invoke-item $logfile
Line 16 filters out the irrelevant forest root entries. Line 24 is a simple LDAP escape for when there is a comma in the location field.