Guy’s Scripting Ezine 55 – Select Case Part 2

Contents for Guy’s Scripting Ezine 55 – Select Case (2)

 ♣

This Week’s Secret

This Week’s Secret is that Guy is a closet puzzler.  My Times newspaper has recently introduced a mathematic quiz called Su Doku.  I have been devising a spreadsheet to solve the problem.  The rule is that each line and each mini-square must contain a single occurrence of each of the first nine numbers.  What I am using to solve Su Doku is Excel with multiple If, statements.

Now the point of this story is that when it comes to computing multiple choice scenarios, my reflex is to employ If, Or, and Lookup.  However, I turn to scripting, we have the wonderful construction: Select Case.

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 – Select Case and LDAP Properties

What I am suggesting is that many of my scripts can be seen as templates and adapted to variety of similar scenarios. So as part of my drive to give each script the widest possible reach, here is a three stage approach to this week’s script.

1)  To orientate to Active Directory

2)  To understand LDAP

3)  To understand Select Case

As I have mentioned before, when ever people write in with their scripting problems, they assume that I know their situation.  When people email me, they believe I can see what they can see.  I want to learn from that communication breakdown and provide background and context for my scripts.

1)  To orientate to Active Directory

All our scripting is going to happen in an OU called accounts.  So either create an accounts OU in your Active Directory Users and Computers, or else change line 15 in my script: strContainer OU=, to an OU that already exists in your domain.

To complete your preparation, add a handful of users each with a value in the Description box.  To match my logic you could choose a nationality for their description.  Example, Name = Dai Jones, Description = Welsh.  Sheila Jones, Description = Australian,  Pierre Jones, Description = French.

2)  To understand LDAP property physicalDeliveryOfficeName

The rational behind this script is to exploit LDAP information that already exists in one field, to populate another LDAP field.  In my example we know the nationality from the Description, the whole reason for this script is to employ VBScript to add an office corresponding to each nationality.  To summarise, in the Case where Description = Australian, then LDAP Office = Brisbane.

Now in LDAP ‘speak’ firstname = givenName, surname = sn, fortunately description = description.  The burning question was what is the LDAP for the Office field?  I tried Office = office, but that did not work.  So what I did was manually add the value of Cardiff to Dai Jones Office, then used the wonderful ADSIEdit to find the corresponding property.  The answer was physicalDeliveryOfficeName. If you are in any doubt check on line. 

3) Select Case (not Case Select)

Select case needs a primary variable to work with. I chose the name varUserDes.  My script sets this varUserDes variable to the description of each user.  So in the case of Dai Jones, varUserDes = "Welsh".

The whole rational behind Select Case is to set a second variable, my second variable is strOffice.  What Select Case is doing is this, If varUserDes = "Welsh", then strOffice = "Cardiff".

Common sense tells us that there will be more than one nationality in our Active Directory users, so we need a Case for each country.  By the way note that each of the statements starts with Case.

I was particularly pleased with Case Else = "Homeless".  What delighted me was the ability to have a ‘catch-all’ to cope with those cases where I had forgotten or overlooked setting a description.


As ever Tools4Ever provide the solution to network puzzles Tools4Ever


Example Script featuring Select Case

Instructions

  1. Important: Which OU will use?  My script uses OU=accounts.  Either create such an OU, or change the script to reflect YOUR ou=.
  2. Make sure that your OU has 4 or 5 users.  Type in a description for each user, best would be to set that description to a country. For Example Description = "English"
  3. Copy and paste the script below into notepad.
  4. Save the file with .vbs extension e.g. SelectCase.vbs.
  5. Double click and examine Active Directory Users and Computers.  In particular see how the Office property is changed.
 

‘ Set SelectCase.vbs
‘ Purpose to set the Office attribute in AD
‘ VBScript to Case Descriptions in a named OU
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 2.3 – December 5th 2004
‘ ———————————————————-‘
Option Explicit
Dim objOU, objUser, objRootDSE, intCounter, varUserDes
Dim strContainer, strOffice, strDNSDomain, strDescription
‘ Binding Part 1 – to Active Directory
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")

‘ If necessary, change the OU=accounts to your choice
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
strOffice = "School"

‘ For each …. loop.
For each objUser in objOU
If objUser.Class = "user" Then

‘ =========================================
‘ Select Case (Not Case Select!)
‘ Introduce a variable, for example varUserDes
varUserDes = objUser.Description
Select Case varUserDes
Case "American" strOffice = "New York"
Case "English" strOffice = "London"
Case "French" strOffice = "Paris"
Case "Canadian" strOffice = "Toronto"
Case "Australian" strOffice = "Brisbane"
Case "German" strOffice = "Berlin"
Case "Welsh" strOffice = "Cardiff"
‘ Finish with Case Else in order to catch exceptions
Case Else strOffice = "Homeless!"
End Select
‘ =========================================
‘ Select case has finished, now set value for Office
objUser.physicalDeliveryOfficeName = strOffice
objUser.SetInfo
intCounter = intCounter + 1

‘ Note how we close the ‘If Then and also the For each loop
End if
next
WScript.echo intCounter & " Descriptions changed "

WScript.Quit

‘ End of example VBScript

Learning Points

Note 0: If all your users have Office = "Homeless" then reset their Description to English, American or German and run the script again.

Note 1: See how the script ‘binds’ to Active Directory.  So there is no need to explicitly add your domain name, the script finds it automatically.

Note 2: To filter out just users, as apposed to computers, I used the If objUser = "User" command.  I could not resist one If statement!

Note 3: It is important to realize how the information actually gets added to Active Directory, so investigate the objUser.SetInfo on line 46.

Note 4:  Most of the learning points were covered in section before the script, now that you have run the script, check back through to see how it works.

Challenges

 

1) Add more Case statements, for example "Italian" strOffice = "Rome"

2) Research other LDAP attributes that you could alter, for example Department.

For extra computer and network tools, checkout Tools4Ever

Summary Select Case

In cases where a script runs through multiple choices, Select Case is logical extension of the If statement.  Once you get to 3 else’s in an If command, it has to be more efficient to use Select Case.  Always add a Case Else statement to catch any values that you have forgotten.

See more about VBScript techniques

VBScripts  • WMI  • Ezines  • Logon Scripts  • Tool Kit  •SLA Monitor  • Ezine 26 Msg Box

Ezine 41 VBS Select case  • Ezine 46 MsgBox  • Free Response Time Tool

Ezine 55 VBS Select case  • Ezine 61 Objects methods   •Ezine 70 GetEx Split