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.

Guy Recommends:  SolarWinds’ Free Bulk Import ToolFree Download Solarwinds Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD’s attributes, click and import the users.

Optionally, you can provide the name of the OU where the new accounts will be born. Download your FREE bulk import tool.

If you need more comprehensive software, download a free trial of SAM (Server & Application Monitor)

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 https://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.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Orion performance monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

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.

See more about VBScript techniques

VBScripts  • WMI  • Ezines  • Logon Scripts  • Tool Kit  •Free IP Tracker  •Free VM Manager

  • Ezine 89 Sendkeys  • Ezine 97 Net Time  • Ezine 98 W32 Time  • Ezine 99 Time Services

Ezine 120 Sendkeys  •Ezine 102 VBScript methods  • Ezine 106 External Arguments