PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 102 - Collect VBScript Methods

Contents for Ezine 102 - Collect VBScript Methods

This Week's Secret

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

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


Example Script: To Modify the Properties of Computer Objects

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.

  1. Copy and paste the example script below into notepad or use a VBScript editor.

  2. 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.

  3. Save the file with a .vbs extension, for example: Description.vbs 

  4. Double click Description.vbs, the check Active Directory Users and Computers for new values for your computer's properties.

  5. 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")

Summary - Methods

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.

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

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.


 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2010 Computer Performance LTD All rights reserved

Please report a broken link, or an error.