Contents for Guy’s Scripting Ezine 47- .SetInfo and .Put
♣ This Week’s SecretWhen 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)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 goalsTo 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
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 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 .PutInstructions
‘ Set SetInfo.vbs ‘ Change the OU= if necessary ‘ Binding Part 2 – from the Root to the very OU ‘ Cycle through User accounts with: For each …. loop. ‘ End of example VBScript Learning PointsNote 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" 2) Amend the Echo statement line like this (note use of underscore) WScript.echo intCounter & " Descriptions changed to " _ 3) A tough challenge, add new properties, for example .Password. Check LDAP properties here. Example 2: Method B – Just .SetInfo‘ Set SetInfo.vbs ‘ Change the OU= if necessary ‘ For each …. loop. WScript.Quit ‘ End of example VBScript Learning PointsNote 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". ChallengeIntroduce a variable called strDept. Declare it, then set its value, finally substitute strDept for "Science" in the script. SummaryTrying 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 |