Script for Bulk Import of Active Directory Subnets

I’ve been using this script for years now and was reminded of it by a post on the activedir.org DL the other day. The script does exactly what the name implies – it takes a tab separated input file (supplied as the first argument) and generates Active Directory subnet objects for each line. If the subnet already exists, the associated site and description will be updated. The script targets the forest the user is currently logged in to.

The code is pasted in below, note the format for the input file (TSV). One field I noticed is missing is the canonical location field. You should be able to add this to the script pretty easily if you need this, or if there’s sufficient demand leave a comment and I can do it.

Note: You can export tab separated files from Excel via the File>Save As menu.
'==========================================================================
' NAME: Import Subnets from Tab Seperated File
'
' AUTHOR: Brian Desmond, brian@briandesmond.com
'
' COMMENT: 
'
' TEMPLATE FILE FORMAT (tab delimited):
' Subnet Address	Prefix Length	Site Name	Description
'==========================================================================

Option Explicit

If WScript.Arguments.Count < 1 Then
	WScript.Echo "Specify an input file name as an argument to this script."
	WScript.Quit(1)
End If 

Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

Dim importFile
importFile = Trim(WScript.Arguments(0))

If Not fso.FileExists(importFile) Then
	WScript.Echo "Input file not found"
	WScript.Quit(1)
End If 

Dim configNcDn
configNcDn = GetConfigNc()

Dim inputReader
Set inputReader = fso.OpenTextFile(importFile)

Dim line
While Not inputReader.AtEndOfStream
	line = inputReader.ReadLine
	Dim tokens
	tokens = Split(line, vbtab)
	
	Dim subnetAddress
	subnetAddress = ""
	Dim prefixLength
	prefixLength = ""
	Dim siteName
	siteName = ""
	Dim description
	description = ""	
	
	On Error Resume Next
	
	subnetAddress = tokens(0)
	prefixLength = tokens(1)
	siteName = tokens(2)
	description = tokens(3)
	
	On Error GoTo 0 
	
	If subnetAddress = "" Or prefixLength = "" Or siteName = "" Then
		WScript.Echo "FAIL: " & line
	Else
		Dim siteDn
		siteDn = GetSiteDn(configNcDn, siteName)
		
		If siteDn = "" Then 
			WScript.Echo "SITE NOT FOUND: " & line 
		Else
			Dim subnetCn
			subnetCn = subnetAddress & "/" & prefixLength
		
			'On Error Resume Next 
			Dim subnetDn
			subnetDn = ""
			subnetDn = GetSubnetDn(configNcDn, subnetCn)
			WScript.Echo subnetDn 
			Dim subnetExists		
			If subnetDn = "" Then
				subnetExists = False
			Else
				subnetExists = True 
			End If 
			
			'Err.Clear 
			'On Error GoTo 0 
			
			Dim subnetObj
			If subnetExists Then 
				WScript.Echo subnetDn 
				Set subnetObj = GetObject("LDAP://" & Replace(subnetDn, "/", "\/"))
			Else 
				Dim configObj
				Set configObj = GetObject("LDAP://CN=Subnets,CN=Sites," & configNcDn)
				
				Set subnetObj = configObj.Create("subnet", "cn=" & subnetCn)
			End If 			

			subnetObj.Put "siteObject", siteDn
			If Not Trim(description) = "" Then 
				subnetObj.Put "description", description
			End If 
			subnetObj.SetInfo 
				
			WScript.Echo "SUCCEED: " & line 
			Set subnetObj = Nothing 
		End If 
	End If 
Wend

inputReader.Close
WScript.Echo "Complete"

Function GetConfigNc()
	Dim rootDse
	Set rootDse = GetObject("LDAP://RootDSE")
	
	Dim configNc
	configNc = rootDse.get("configurationNamingContext")
	
	Set rootDse = Nothing
	
	GetConfigNc = configNc
End Function

Function GetSiteDn(configNc, siteName)
	Dim cnxn
	Set cnxn = WScript.CreateObject("ADODB.Connection")
	cnxn.Provider = "ADsDSOObject"
	cnxn.Open "Active Directory Provider"
	
	Dim cmd
	Set cmd = WScript.CreateObject("ADODB.Command")
	cmd.ActiveConnection = cnxn
	
	cmd.CommandText = "<LDAP://" & configNc & ">;(&(objectcategory=site)(cn=" & siteName & "));distinguishedName;subtree"
	cmd.Properties("Page Size") = 100
	cmd.Properties("Timeout") = 30
	cmd.Properties("Cache Results") = False
	
	Dim rs
	Set rs = cmd.Execute
	
	While Not rs.eof 
		GetSiteDn = rs.fields("distinguishedName").Value
		
		rs.MoveNext
	Wend 
	
	rs.close
	cnxn.Close
	
	Set rs = Nothing
	Set cmd = Nothing
	Set cnxn = Nothing 
End Function 

Function GetSubnetDn(configNc, subnetName)
	Dim cnxn
	Set cnxn = WScript.CreateObject("ADODB.Connection")
	cnxn.Provider = "ADsDSOObject"
	cnxn.Open "Active Directory Provider"
	
	Dim cmd
	Set cmd = WScript.CreateObject("ADODB.Command")
	cmd.ActiveConnection = cnxn
	
	cmd.CommandText = "<LDAP://" & configNc & ">;(&(objectcategory=subnet)(cn=" & subnetName & "));distinguishedName;subtree"
	cmd.Properties("Page Size") = 100
	cmd.Properties("Timeout") = 30
	cmd.Properties("Cache Results") = False
	
	Dim rs
	Set rs = cmd.Execute
	
	While Not rs.eof 
		GetSubnetDn = rs.fields("distinguishedName").Value
		
		rs.MoveNext
	Wend 
	
	rs.close
	cnxn.Close
	
	Set rs = Nothing
	Set cmd = Nothing
	Set cnxn = Nothing 
End Function

Posted Saturday, February 21 2009 3:02 PM by Brian Desmond | 8 Comments
Tagged as: , ,

Comments, Trackbacks, & Pingbacks

#1 re: Script for Bulk Import of Active Directory Subnets

Monday, April 20 2009 12:27 PM by Jason

Thanks for this. Unfortunately, I can't seem to get it working. Do you think you could post specific instructions on how to use it? I think I may be doing it incorrectly. I'm not even sure what kind of file it needs to be. I made it a vbs file and tried running from a command line with the tsv file specified. It says SITE NOT FOUND with the info from the tsv file in a WSH dialog box and then says Complete. But the subnet does not get added. Any help would be greatly appreciated.

Adding the Canonical Location field would also be a great help. Thanks again!

#2 re: Script for Bulk Import of Active Directory Subnets

Monday, April 20 2009 12:33 PM by Jason

Ok....I figured out the problem. I was trying in one of our smaller forests that didn't have the particular site specified in the tsv file. I changed it and it worked. The location field would still be a great help.

Thanks again this is very helpful!!

#3 re: Script for Bulk Import of Active Directory Subnets

Thursday, April 30 2009 3:47 PM by Don Kuhlman

Worked like a champ! I ran it to create 118 subnets and didn't have any issues.

Nice job and thanks Brian!

#4 re: Script for Bulk Import of Active Directory Subnets

Saturday, June 27 2009 1:07 AM by robert

thank you very much... this was very useful script..

#5 re: Script for Bulk Import of Active Directory Subnets

Saturday, July 04 2009 10:32 PM by Scoobies

Looks excellent - I need this for 100+ subnets soon.

It would be good if the script also inlcuded a setting for the "Location" field.

#6 re: Script for Bulk Import of Active Directory Subnets

Thursday, August 20 2009 1:56 PM by Kris

This script is nice, but can you possibly post or email me a script which can export all the existing subnets for a particular site with the following fields as well:

1. Description

2. Location

Thanks in advance

Kris

#7 re: Script for Bulk Import of Active Directory Subnets

Thursday, September 17 2009 1:45 PM by Matt

Script works great!!! This saved me hours of time. It was easy to add the location.

#8 re: Script for Bulk Import of Active Directory Subnets

May 18, 2010 12:26 AM by Chevrolet Lumina Apv Bulb Fog Light, Lumina Montebello

Pingback from Chevrolet Lumina Apv Bulb Fog Light, Lumina Montebello

Leave a comment