AllExperts > VB Script 
Search      
VB Script
Volunteer
Answers to thousands of questions
 Home · More VB Script Questions · Answer Library  · Encyclopedia ·
More VB Script Answers
Question Library

Ask a question about VB Script
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About 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

 
   

You are here:  Experts > Computing/Technology > Basic > VB Script > vb script modify question

VB Script - vb script modify question


Expert: Subbu - 5/4/2009

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.

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.