You are here:

VB Script/vb script modify question

Advertisement


Question
Hello, I have this script which modifies the site assignment for a subnet in Active Directory:

' This code updates the site assignment of a subnet object.
' ------ SCRIPT CONFIGURATION ------
strNewSiteName = "<SiteName>" ' e.g. "Raleigh"
strSubnetName = "<SubnetName>" ' e.g. "192.168.1.0/24"
' ------ END CONFIGURATION ---------
    
set objRootDSE = GetObject("LDAP://RootDSE")
set objSiteSettings = GetObject("LDAP://cn=" & _strSubnetName & _
                               "cn=subnets,cn=sites," & _
                               objRootDSE.Get("ConfigurationNamingContext"))
    
objSiteSettings.Put "siteObject", _
                   "cn=" & strNewSiteName & ",cn=sites," & _
                   objRootDSE.Get("ConfigurationNamingContext")
objSiteSettings.SetInfo
WScript.Echo("Site membership updated successfully!")

What I want to do is to modify this script to change the site assignment to multiple subnets. I have a list of subnets that need to be assigned to the same site so the site info remains unchanged.
The problem is I don`t know how to do a loop to read multiple subnets which I instert in the code or from a txt file.

Can you help me please...I am despereate because I have to do this very fast.

Many thanks in advance!

Answer
Hi Raducu,

Here is the code for just reading a text file:
' Get a free file number
nFileNum = FreeFile

' Open a text file for input
Open "C:\test.txt" For Input As nFileNum


' Read the contents of the file
Do While Not EOF(nFileNum)

  Line Input #nFileNum, snextline
  'MsgBox snextline

Loop
' Close the file
Close nFileNum


Here I just updated with your code:
' Get a free file number
nFileNum = FreeFile

' Open a text file for input
Open "C:\subnet.txt" For Input As nFileNum


' Read the contents of the file
Do While Not EOF(nFileNum)

  strNewSiteName = "<SiteName>" ' e.g. "Raleigh"
  Line Input #nFileNum, strSubnetName
  'MsgBox snextline
  set objRootDSE = GetObject("LDAP://RootDSE")
  set objSiteSettings = GetObject("LDAP://cn=" & _strSubnetName & _
                              "cn=subnets,cn=sites," & _
                              objRootDSE.Get("ConfigurationNamingContext"))
   
  objSiteSettings.Put "siteObject", _
                  "cn=" & strNewSiteName & ",cn=sites," & _
                  objRootDSE.Get("ConfigurationNamingContext")
  objSiteSettings.SetInfo
  WScript.Echo("Site membership updated successfully!")

Loop
' Close the file
Close nFileNum


---------------------------------------------------------------------
Please make sure your subnet.txt file data should contain like following:

192.168.1.0/24
192.168.1.0/25
192.168.1.0/26
192.168.1.0/27
192.168.1.0/28
192.168.1.0/29

Do not add any header line and dont give enter key after last subnet name.

Feel free to contact me for any other questions.

Subbu.

VB Script

All Answers


Answers by Expert:


Ask Experts

Volunteer


Subbu

Expertise

I can answer VB Script questions which are related to Visual Basic / VBA / QTP Scripts.

Experience

8 Years

Education/Credentials
Master of Computer Applications

Awards and Honors
Established Member from QA Forums and Brainbench certification

©2012 About.com, a part of The New York Times Company. All rights reserved.