Guy’s Scripting Ezine No 47 – .SetInfo and .Put

Contents for Guy’s Scripting Ezine 47- .SetInfo and .Put

 ♣

This Week’s Secret

When I discover two ways of achieving the same goal, it gives me perspective.  By comparing the two methods I gain true understanding of the process.  VBScripts are a rich source of multiple techniques, for example, no two people script MapNetworkDrive in quite the same way.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

This Week’s Mission.

Let us pretend that our job depends on being able to change the properties of hundreds of users in Active Directory.  For example it’s a new college term and the student’s accounts all need their descriptions changing from year 1 to year 2. Luckily for us, all the users are in the same OU.

Scripting goals

To employ two different methods to achieve the same goal.  Either we can change the user’s properties with ObjUser.SetInfo, or we can use ObjUser.Put.

Method A – .SetInfo and .Put

Method A focuses on .Put, and relies on two further arguments, one for the name of the property and one for the actual value.  It surprised me that you still needed the .SetInfo after your last .Put statement.

    ObjUser.Put "Description", "Year 2"       ‘ Two arguments
    ObjUser.SetInfo

 

Method B – Just .SetInfo

Method B just uses .SetInfo, but relies on a dot property command for example: .Description  or .Department.

    ObjUser.Description = "Year 2"         ‘ Just one arguement
    ObjUser.SetInfo

Perspective and Preamble

From a scripting point of view there is more to changing the user’s property than one statement :ObjUser.Put.  Let us take time to see how the rest of the script is constructed.

To help you dissect the script I have added comments.  If you have a classy script editor like VBsEdit, these comments will be colour coded green.

Since these users are Active Directory objects, the first part of the script binds to the LDAP root.  Indeed, the acronym LDAP reminds us that we are scripting Active Directory properties of the user object.

The most important practical point, is to make sure that your value for strContainer = "OU=Accounts ," is correct.  Not only perfecting the syntax of the those equal signs, but also checking that the OU actually exists in YOUR Active Directory.

It almost goes without saying that you need to create a user or two in that OU for the script to be effective!


If you would like an off-the-shelf program to manage Active Directory and much more, then check out:Tools4Ever


Example 1: Method A – .SetInfo and .Put

Instructions

  1. Copy and paste the script below into notepad.
  2. Save the file with .vbs extension e.g. SetInfo.vbs.
  3. Double click and examine the message box.
  4. Check out Active Directory Users and Computers.
  5. Beware the refresh trap, F5 does not always work, so make sure you can find ‘Refresh’ from the short cut menu.

‘ Set SetInfo.vbs
‘ VBScript to Set Descriptions and Departments in a named OU
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 2.3 – September 27th 2004
‘ —————————————————————–‘
Option Explicit
Dim objOU, objUser, objRootDSE, intCounter
Dim strContainer, strLastUser, strDNSDomain
‘ Binding Part 1 – to Active Directory
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")

‘ Change the OU= if necessary
strContainer = "OU=accounts ,"

‘ Binding Part 2 – from the Root to the very OU
strContainer = strContainer & strDNSDomain
set objOU =GetObject("LDAP://" & strContainer )
intCounter = 0

‘ Cycle through User accounts with: For each …. loop.
For each objUser in objOU
If objUser.class="user" Then
objUser.Put "Description", "Year 3"
objUser.Put "Company", "Acme"
objUser.SetInfo
intCounter = intCounter +1
End if
next
WScript.Echo intCounter & " Descriptions changed to Year 3"
WScript.Quit

‘ End of example VBScript
 

Learning Points

Note 1:  Be aware that:   .Put "Description" is correct.  I was beginning to go mad because the space in "Description     " meant that my first script failed.  I draw your attention to this phantom space so that you do not repeat my mistake should you try adding other LDAP properties to your script.

Note 2: In addition to changing the Description, the script also changes the Company name which you will see under the Organization Tab of the User property sheet. 

Note 3: The power of the script comes from ‘looping.  This is the statement responsible for cycling through all the accounts: For each… next.  Also note the IF statement in this section which filters out Users from other objects in the OU. (Such as Computers.)

Note 4: I added intCounter to keep track of how many times the script loops.

Challenges:

If you like a challenge, then try out these amendments to the script.

1) Declare, then use a variable called strDescription

Set strDescription = "Year 3"

Change:         objUser.Put "Description", "Year 2"
To:                objUser.Put "Description", strDescription
 

2) Amend the Echo statement line like this (note use of underscore)

      WScript.echo intCounter & " Descriptions changed to " _
      & strDescription.

3) A tough challenge, add new properties, for example .Password.  Check LDAP properties here.

LDAP Properties

Example 2: Method B – Just .SetInfo

‘ Set SetInfo.vbs
‘ VBScript to Set Descriptions and Departments in a named OU
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 3.5 – September 27th 2004
‘ —————————————————————–‘
Option Explicit
Dim objOU, objUser, objRootDSE, intCounter
Dim strContainer, strLastUser, strDNSDomain, strDescription
‘ Binding Part 1 – to Active Directory
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")

‘ Change the OU= if necessary
strContainer = "OU=accounts ,"
strDescription = "Class A"
‘ Binding Part 2 – from the Root to the very OU
strContainer = strContainer & strDNSDomain
set objOU =GetObject("LDAP://" & strContainer )
intCounter = 0

‘ For each …. loop.
For each objUser in objOU
If objUser.class="user" Then
objUser.Description = strDescription
objUser.Department = "Science"
objUser.SetInfo
intCounter = intCounter +1
End if
next
WScript.echo intCounter & " Descriptions changed to " _
& strDescription

WScript.Quit

‘ End of example VBScript
 

Learning Points

Note 1:  Guy incorporated the strDescription variable in this script.  You may know how I love to use variables in scripts.

Note 2:  I decided to introduce another property: .Department and set its value to "Science".

Challenge

Introduce a variable called strDept.  Declare it, then set its value, finally substitute strDept for "Science" in the script.

Summary

Trying two different methods to achieve the same goal, helps you to gain perspective and true understand the underlying process.  However, I admit that when it comes to writing the final production script, you only need one method.  You choose which one!

See more about VBScript

VBScripts  • Ezines  • WMI  • Logon Scripts   • PowerShell Foreach  •Tool Kit

• Ezine 1 Loops   •Ezine 14 Loops  • Ezine 47 .put  • PowerShell Loops  • Free Web Watcher

• Ezine 90 VBS Looping  • Ezine 91 VBS Looping   • Ezine 92 LDIFDE   • Ezine 100 If