Contents for Ezine 102 - Collect VBScript Methods
This week's secret is never miss a chance to collect a VBScript method. In the scripting model, objects such as user or computer are important, however, it's the methods that convert plans into actions. Next
week we will focus on the LDAP properties, for example, givenName, sAMAccountName and displayName, but this week it's methods, such as .Put and .SetInfo that are caught in my spotlight.
I think of methods as the doers, the men of action. As I write my web pages, I search for the verb with just the correct shade of meaning, when I script I seek methods, which perform just the right action.
This week's mission is to modify the computer object's properties. I choose the computer because it's often a neglected object, also I want to re-enforce the point that all Active Directory objects have
more properties than you can imagine.
My hidden agenda is to alert you to the presence of two properties with similar names, description and displayName. As another aside, you can script information about the department to which your
computers' belong.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
Pre-requisites You need an OU called Accounts, else change my value for strContainer. It makes sense to create or move two or three computer objects in the OU specified by the above
strContainer. This script is designed for Windows Active Directory. You really need to run this script on a domain controller rather than
an XP workstation.
Instructions for modifying the properties of computer objects. Copy and paste the example script below
into notepad or use a VBScript editor.
One advantage of a good script editor such as OnScript is that you can see the line numbers, which helps when you have to troubleshoot error messages.
Save the file with a .vbs extension, for example:
Description.vbs
Double click Description.vbs, the check Active Directory Users and Computers for new values for your computer's properties. N.B. It may be
necessary to right click the OU and select 'Refresh' from the drop down menu. F5 is only works the first time then goes silent.
' Description.vbs ' VBscript with methods to modify computer properties ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 1.2 - February 2006 '
----------------------------------------------------------' Option Explicit Dim objRoot, objDomain, objOU, objComputer Dim strContainer, intCounter
strContainer ="OU=Accounts,"
' Section
to bind to ANY Active Directory. Set objRoot = GetObject("LDAP://rootDSE") objDomain = objRoot.Get("defaultNamingContext") strContainer = strContainer & objDomain set objOU =GetObject("LDAP://" &
strContainer ) intCounter = 0
' Start of Loop. ' Note filter: If then... For each objComputer in objOU If objComputer.class="computer" Then
objComputer.Put "Description", "Laptop" objComputer.Put "DisplayName", "Toshiba" objComputer.SetInfo intCounter = intCounter
+1 End if next WScript.Echo intCounter & " Descriptions changed to Laptop" WScript.Quit
Learning Points
Note 1: The first method, .Put is like typing (or pasting) the values into the computer's property sheet; while the second method, .SetInfo is like pressing the OK button when you have finished modifying that
object. Incidentally last week we employed the .Get and .Create methods.
Get into the rhythm of modifying object with methods. For example objComputer.Put, then the name of the property "Description", finally the value "Desktop".
Note 2: Following on from the previous ezine, this script binds to Active Directory then obtains the name of your domain, there is no need to hard-code dc=xyz. Nevertheless, do pay attention to strContainer,
for instance this script requires a comma, after the name of the OU.
Note 3: Observe the simple but effective: For Each .... Next loop.
Note 4: This script uses the If ... then ... End If, to filter just computer objects.
Challenges
1)
My Active Directory challenge is to add more properties, for example: objComputer.Put "Department", "Human Resources".
2)
My scripting challenge is to introduce more variables, for example: Dim strDescription strDescription = "Acme Computers" objComputer.Put "Description", strDescription (in place of: objComputer.Put
"Description", "Desktop")
Never miss the chance to learn a new VBScript method. Whenever you see a new script, by all means 'parse' it for the names of objects or properties, but in particular seek the methods. For example .Put and .SetInfo.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
|