2013/11/11

PowerShell - Add AD Site Subnet

Last month I posted a script that report the Missing Subnets from the Active Directory. The script goes on each Domain Controllers and get the last x entries from the NETLOGON.log file.
Once this report is generated, you might want to check with your Telecom guy/team to get the correct network mask, correct site of each entries and fix this situation.

Reminder: Subnet objects (class subnet) define network subnets in Active Directory. A network subnet is a segment of a TCP/IP network to which a set of logical IP addresses is assigned. Subnets group computers in a way that identifies their physical proximity on the network. Subnet objects in Active Directory are used to map computers to sites.

Today I will show how to add those missing subnets in your Active Directory using PowerShell on Windows Server 2012 and Previous versions via ADSI.


Using ADSI

Applies To: Windows XP or higher, Windows PowerShell 2.0 or higher, Windows Server 2003 or higher.

In some of my previous posts, I learned a lot about ADSI and I thought I could do the same for this one and find a way to create my subnets with ADSI.

I came up with the following function which accept 4 parameters: Subnet, SiteName, Description and Location.

Download from TechNet Gallery
Download from Github (CSV and PS1)


PROCESS{
        TRY{
            $ErrorActionPreference = 'Stop'
            
            # Distinguished Name of the Configuration Partition
            $Configuration = ([ADSI]"LDAP://RootDSE").configurationNamingContext

            # Get the Subnet Container
            $SubnetsContainer = [ADSI]"LDAP://CN=Subnets,CN=Sites,$Configuration"
            
            # Create the Subnet object
            Write-Verbose -Message "$subnet - Creating the subnet object..."
            $SubnetObject = $SubnetsContainer.Create('subnet', "cn=$Subnet")
        
            # Assign the subnet to a site
            $SubnetObject.put("siteObject","cn=$SiteName,CN=Sites,$Configuration")

            # Adding the Description information if specified by the user
            IF ($PSBoundParameters['Description']){
                $SubnetObject.Put("description",$Description)
            }
            
            # Adding the Location information if specified by the user
            IF ($PSBoundParameters['Location']){
                $SubnetObject.Put("location",$Location)
            }
            $SubnetObject.setinfo()
            Write-Verbose -Message "$subnet - Subnet added."
        }#TRY
        CATCH{
            Write-Warning -Message "An error happened while creating the subnet: $subnet"
            $error[0].Exception
        }#CATCH
}#PROCESS Block

Download on TechNet Gallery
Download from Github (CSV and PS1)

Adding one subnet

PS C:\LazyWinAdmin> Add-ADSubnet -Subnet '192.168.10.0/24' -SiteName 'MTL1' -Verbose
VERBOSE: 192.168.10.0/24 - Creating the subnet object...
VERBOSE: 192.168.10.0/24 - Subnet added.
VERBOSE: Script Completed

Adding a bunch of subnets from a CSV file


We have the following CSV file with a few subnets to add, we can use Import-CSV to create all the subnet at once.

PS C:\LazyWinAdmin> Import-csv .\subnets.csv | Add-ADSubnet -Verbose
VERBOSE: 192.168.1.0/24 - Creating the subnet object...
VERBOSE: 192.168.1.0/24 - Subnet added.
VERBOSE: 192.168.2.0/24 - Creating the subnet object...
VERBOSE: 192.168.2.0/24 - Subnet added.
VERBOSE: 192.168.3.0/24 - Creating the subnet object...
VERBOSE: 192.168.3.0/24 - Subnet added.
VERBOSE: 192.168.4.0/24 - Creating the subnet object...
VERBOSE: 192.168.4.0/24 - Subnet added.
VERBOSE: 192.168.5.0/24 - Creating the subnet object...
VERBOSE: 192.168.5.0/24 - Subnet added.
VERBOSE: 192.168.6.0/24 - Creating the subnet object...
VERBOSE: 192.168.6.0/24 - Subnet added.
VERBOSE: 192.168.7.0/24 - Creating the subnet object...
VERBOSE: 192.168.7.0/24 - Subnet added.
VERBOSE: 192.168.8.0/24 - Creating the subnet object...
VERBOSE: 192.168.8.0/24 - Subnet added.
VERBOSE: Script Completed

The parameters of my function NameLocation, Site and Description will match the properties in the CSV file so the cmdlet will be able to interpret them. This is possible thanks to the  parameter ValueFromPipelineByPropertyName

Management Console: Active Directory Sites and Services. We can see the
subnets created.

Download the function

TechNet Gallery
Github (CSV and PS1)



Using the new cmdlets in ActiveDirectory module

Applies To: Windows 8.1, Windows PowerShell 4.0, Windows Server 2012 R2

Finding related cmdlets

PS C:\LazyWinAdmin> get-command *subnet*
CommandType     Name                                               ModuleName
-----------     ----                                               ----------
Cmdlet          Get-ADReplicationSubnet                            ActiveDirectory
Cmdlet          New-ADReplicationSubnet                            ActiveDirectory
Cmdlet          Remove-ADReplicationSubnet                         ActiveDirectory
Cmdlet          Set-ADReplicationSubnet                            ActiveDirectory

Get the current subnets

PS C:\LazyWinAdmin> Get-ADReplicationSubnet -Filter *
DistinguishedName : CN=10.1.0.0/22,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB
Location          : Montreal, Canada
Name              : 10.1.0.0/22
ObjectClass       : subnet
ObjectGUID        : 98683337-da77-412e-ae57-9fc0dbb209ba
Site              : CN=FX3,CN=Sites,CN=Configuration,DC=FX,DC=LAB

DistinguishedName : CN=10.2.0.0/22,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB
Location          : Montreal, Canada
Name              : 10.2.0.0/22
ObjectClass       : subnet
ObjectGUID        : fa21c05b-40da-4746-b210-60eed2c239fb
Site              : CN=MTL1,CN=Sites,CN=Configuration,DC=FX,DC=LAB


Adding a New subnet

PS C:\LazyWinAdmin> New-ADReplicationSubnet -Name '10.0.0.0/22' -site 'FX3' -Location 'Europe' -PassThru
DistinguishedName : CN=10.0.0.0/22,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB
Location          : Europe
Name              : 10.0.0.0/22
ObjectClass       : subnet
ObjectGUID        : b88d7b53-fa96-4454-8978-13ab032a0a16
Site              : CN=FX3,CN=Sites,CN=Configuration,DC=FX,DC=LAB


Adding a bunch of subnets

We re-use the same file used in the ADSI example (above) with a few subnets to add:

Name, Location, Site and Description properties are in the CSV file so the cmdlet will be able to interpret them. 

Get-Help New-ADReplicationSubnet -ShowWindow
Here is the result using the -Verbose parameter.
By default New-ADReplicationSubnet cmdlet does not generate output, so here we only see the output of the verbose parameter.


PS C:\LazyWinAdmin> import-csv .\subnets.csv | New-ADReplicationSubnet -Verbose
VERBOSE: Performing operation "New" on Target
"CN=192.168.1.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".
VERBOSE: Performing operation "New" on Target
"CN=192.168.2.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".
VERBOSE: Performing operation "New" on Target
"CN=192.168.3.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".
VERBOSE: Performing operation "New" on Target
"CN=192.168.4.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".
VERBOSE: Performing operation "New" on Target
"CN=192.168.5.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".
VERBOSE: Performing operation "New" on Target
"CN=192.168.6.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".
VERBOSE: Performing operation "New" on Target
"CN=192.168.7.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".
VERBOSE: Performing operation "New" on Target
"CN=192.168.8.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".

The parameter -PassThru must be used if you want to see the output of this cmdlet.

PS C:\LazyWinAdmin> import-csv .\subnets.csv | New-ADReplicationSubnet -PassThru -Verbose
VERBOSE: Performing operation "New" on Target
"CN=192.168.1.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".
DistinguishedName : CN=192.168.1.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB Location : Paris Name : 192.168.1.0/24 ObjectClass : subnet ObjectGUID : 88f6be31-5f56-48bc-9986-fd3afe15cac9 Site : CN=FX2,CN=Sites,CN=Configuration,DC=FX,DC=LAB
VERBOSE: Performing operation "New" on Target
"CN=192.168.2.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".
DistinguishedName : CN=192.168.2.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB Location : London Name : 192.168.2.0/24 ObjectClass : subnet ObjectGUID : b2539cc1-7bff-4f62-bf17-29e7ac94dbbe Site : CN=FX3,CN=Sites,CN=Configuration,DC=FX,DC=LAB
VERBOSE: Performing operation "New" on Target
"CN=192.168.3.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".
DistinguishedName : CN=192.168.3.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB Location : Montreal Name : 192.168.3.0/24 ObjectClass : subnet ObjectGUID : 7f08aa1a-ad34-428b-9138-bf18e14d0610 Site : CN=MTL1,CN=Sites,CN=Configuration,DC=FX,DC=LAB
VERBOSE: Performing operation "New" on Target
"CN=192.168.4.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".
DistinguishedName : CN=192.168.4.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB Location : London Name : 192.168.4.0/24 ObjectClass : subnet ObjectGUID : 3bd6e017-9974-4eea-9dfd-a0dd3739b65f Site : CN=FX3,CN=Sites,CN=Configuration,DC=FX,DC=LAB
VERBOSE: Performing operation "New" on Target
"CN=192.168.5.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".
DistinguishedName : CN=192.168.5.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB Location : London Name : 192.168.5.0/24 ObjectClass : subnet ObjectGUID : 26c43a45-868d-4122-91f8-f2385e505bcf Site : CN=FX3,CN=Sites,CN=Configuration,DC=FX,DC=LAB
VERBOSE: Performing operation "New" on Target
"CN=192.168.6.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".
DistinguishedName : CN=192.168.6.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB Location : Paris Name : 192.168.6.0/24 ObjectClass : subnet ObjectGUID : 1fb55339-1798-4666-9d06-647358558bc2 Site : CN=FX2,CN=Sites,CN=Configuration,DC=FX,DC=LAB
VERBOSE: Performing operation "New" on Target
"CN=192.168.7.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".
DistinguishedName : CN=192.168.7.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB Location : Montreal Name : 192.168.7.0/24 ObjectClass : subnet ObjectGUID : 7ddcdfc3-bb97-41f6-94ab-26439da391d9 Site : CN=MTL1,CN=Sites,CN=Configuration,DC=FX,DC=LAB
VERBOSE: Performing operation "New" on Target
"CN=192.168.8.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB".
DistinguishedName : CN=192.168.8.0/24,CN=Subnets,CN=Sites,CN=Configuration,DC=FX,DC=LAB Location : Paris Name : 192.168.8.0/24 ObjectClass : subnet ObjectGUID : 494113f6-ac7a-4398-a155-2b1d3b400c15 Site : CN=FX2,CN=Sites,CN=Configuration,DC=FX,DC=LAB








Thanks for Reading! If you have any questions, leave a comment or send me an email at fxcat@lazywinadmin.com. I invite you to follow me on Twitter @lazywinadm / Google+ / LinkedIn You can also follow the LazyWinAdmin Blog on Facebook Page and Google+ Page

1 comment:

  1. Hi ! Thank you for all informations. Is it possible to download your csv file please ?

    ReplyDelete