Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Bulk import of subnets into AD Sites and Services with site, subnet and description

Posted on 2010-09-09
3
Medium Priority
?
2,494 Views
Last Modified: 2012-05-10
The script included was created to do the job but I get the popups below.
Line 102 is subnetObj.SetInfo
 
Any idea what I'm missing?



'==========================================================================
' 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

Open in new window

Blank popup WSH error message
0
Comment
Question by:bstillion
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 15

Expert Comment

by:markpalinux
ID: 33655547


Put the following between line 101 and 102 , see what the results are - sometimes I have found that instead of \ you need \\ in the DN names.
WScript.Echo "subnetDn ="  & subnetDn
Wscript.echo "subnetCn = " & subnetCn
Wscript.echo "siteDn = " & siteDn
      
0
 

Author Comment

by:bstillion
ID: 33661572
I get the following after adding the WScript.Echo lines to the script.
The text file that is that provides the needed information is shows the following
172.19.0.14      23       <site>       <Description>




error1.JPG
ErrorSubnetDN.jpg
SubnetCN.JPG
SiteDN.JPG
Line105Error.JPG
0
 

Accepted Solution

by:
bstillion earned 0 total points
ID: 33666949
I found a simple way to populate sites and services using LDIFDE.
The inport file looks like this for each sunet to be added:

dn: cn=172.23.161.0/24,cn=subnets,cn=sites,cn=configuration,dc=mehealthtest,dc=org
changetype: add
ObjectClass: subnet
Location: 111 Maine
Description: Wireless Network
siteObject: cn=Main,cn=sites,cn=configuration,dc=mehealthtest,dc=org

Type this at the command prompt:
ldifde -v -i -f create_subnet.ldf

this article explains it:
http://codeidol.com/active-directory/active-directory/Site-Topology/Creating-a-Subnet/

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question