Export Subnets from Active Directory Sites and Services

As part of an AD upgrade project we have recently been working on, need to discover what existing AD sites were deployed, and whether these needed to be deployed in the new AD environment.

This can be quickly achieved using some simple PowerShell commands

Firstly, we needed to check we could get all the AD sites;

[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites

Then we wanted to add the subnets associated with each AD site;

$sites = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites
 
$sitesubnets = @()
 
foreach ($site in $sites)
{
	foreach ($subnet in $site.subnets){
	   $temp = New-Object PSCustomObject -Property @{
	   'Site' = $site.Name
	   'Subnet' = $subnet; }
	    $sitesubnets += $temp
	}
}
 
$sitesubnets

Now to make good use of this information, we exported this to a CSV file for further analysis;

$sites = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites

$sitesubnets = @()

foreach ($site in $sites)
{
	foreach ($subnet in $site.subnets){
	   $temp = New-Object PSCustomObject -Property @{
	   'Site' = $site.Name
	   'Subnet' = $subnet; }
	    $sitesubnets += $temp
	}
}

$sitesubnets | Export-CSV "c:\temp\subnet.csv"

 

Leave a Reply

Your email address will not be published. Required fields are marked *