Export Sites and Subnets PowerShell to CSV

I came across an issue the other day when setting up a primary site for a customers Regional Office. The issue was that when enabling discovery methods namely the “Active Directory Forest Discovery“. As I’m sure you are aware there is a useful tick box that can be marked to “Automatically create IP address range boundaries for IP subnets when they are discovered“. This although a usually a useful check box caused the Discovery Method to pull back EVERY subnet listed globally… about 2500)!

So to get round this I created this PowerShell Script to export the Sites required and the Subnets associated with the sites in Active Directory Sites and Subnets. This script will export the data to a CSV that can then be imported into ConfigMgr later using this script.

Note: If you run the script without Parameters and just want to enter one Site to export, just hit enter on the next line and it will then ask you for save location.

Note: As in the image at the bottom of this page the save location must include the CSV Name e.g. “C:\temp\sites.csv”

#################################################################################################################################
#Script Name:   Export Specific Sites and Subnets to CSV                                                                        #
#Script Author: SCCMOG - Richie Schuster 06/03/2017 WWW.SCCMOG.COM                                                              #
#################################################################################################################################
#Script Usage: ExportSitesandSubnets -siteNames "Jersey" for single site , seperated "Jersey,Guernsey" -CSVOut C:\temp\site.csv #
#              for multiple.                                                                                                    #
#################################################################################################################################

#Params
Param
(
[parameter(mandatory=$true,HelpMessage="Please, provide Site Name(s). To list more than one site seprate with , e.g. Jersey,Guernsey ")][ValidateNotNullOrEmpty()][String[]]$siteNames,
[parameter(mandatory=$true,HelpMessage="Please, provide a location to save the Exported CSV with the filename. e.g C:\Temp\SCCMOGSites.CSV")][ValidateNotNullOrEmpty()][String]$CSVOut
)

 
#If entery is input run script
If ($siteNames -ne $null){

#Set Lists
[System.Collections.ArrayList]$sites = @()
[System.Collections.ArrayList]$sitesubnets = @()

#Get each site list in sitenames variable and add to variable
for ($i=0; $i -lt $siteNames.Count; $i++){
    $sites += [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites | Where-Object -Property Name -like $siteNames[$i]
    }

#Loop through each site and create simple match 
foreach ($site in $sites){
	foreach ($subnet in $site.subnets){
	   $temp = New-Object PSCustomObject -Property @{
	   'Name' = $site.Name
	   'Subnet' = $subnet; }
	    $sitesubnets += $temp
	}
}
#Export to CSV
$sitesubnets | Export-Csv $CSVOut -Force
}
#If params are not specified then inform.
Else{
    Write-host 'Script Param must be used : "ExportSitesandSubnets -siteNames Jersey" for single site or , seperated "Jersey,Guernsey" for multiple followed by -CSVOut C:\temp\site.csv' -ForegroundColor Yellow
    }
#########################################################################################################

The Image below shows the script being run and the output:

Export Sites and Subnets to CSV PowerShell Script
Export Sites and Subnets to CSV PowerShell Script

5 Replies to “Export Sites and Subnets PowerShell to CSV”

  1. I updated your script a little to make it more efficient (and therefore run faster, and take fewer resources)

    #################################################################################################################################
    #Script Name: Export Specific Sites and Subnets to CSV #
    #Script Author: SCCMOG – Richie Schuster 06/03/2017 http://WWW.SCCMOG.COM
    # slight amendments to make it faster and more efficient (Ernest Brant)
    #################################################################################################################################
    #Script Usage: ExportSitesandSubnets -siteNames “Jersey” for single site , seperated “Jersey,Guernsey” -CSVOut C:\temp\site.csv #
    # for multiple. #
    #################################################################################################################################

    #Params
    Param
    (
    [parameter(mandatory = $true, HelpMessage = “Please, provide Site Name(s). To list more than one site seprate with , e.g. Jersey,Guernsey “)]
    [ValidateNotNullOrEmpty()]
    [String[]]$siteNames,
    [parameter(mandatory = $true, HelpMessage = “Please, provide a location to save the Exported CSV with the filename. e.g C:\Temp\SCCMOGSites.CSV”)]
    [ValidateNotNullOrEmpty()]
    [String]$CSVOut
    )

    #If entery is input run script
    If ($siteNames -ne $null)
    {

    #Set Lists

    $sites = New-Object System.Collections.ArrayList
    $sitesubnets = New-Object System.Collections.ArrayList

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

    #Get each site list in sitenames variable and add to variable
    for ($i = 0; $i -lt $siteNames.Count; $i++)
    {
    [void]$sites.Add($($ForestSites | Where-Object -Property Name -like $siteNames[$i]))
    }

    #Loop through each site and create simple match
    foreach ($site in $sites)
    {
    foreach ($subnet in $site.subnets)
    {
    $temp = New-Object PSCustomObject -Property @{
    ‘Name’ = $site.Name
    ‘Subnet’ = $subnet;
    }
    [void]$sitesubnets.Add($temp)
    }
    }
    #Export to CSV
    $sitesubnets | Export-Csv $CSVOut -Force
    }
    #If params are not specified then inform.
    Else
    {
    Write-host ‘Script Param must be used : “ExportSitesandSubnets -siteNames Jersey” for single site or , seperated “Jersey,Guernsey” for multiple followed by -CSVOut C:\temp\site.csv’ -ForegroundColor Yellow
    }
    #########################################################################################################

Leave a Reply

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

Copyright 2016 SCCMOG | All Rights Reserved

css.php