Guy’s Scripting Ezine 106 – External Arguments

Guy’s Scripting Ezine 106 – External Arguments

 ♣

This Week’s Secret

20 years ago, I spent one of my ‘seven lives’ as a school teacher.  If an interesting topic suddenly turned up, I was never afraid to postpone a planned lesson.  Well this week, I have postponed the intended error correcting topic because Paul DeBrino (www.InfinityRD.com) sent me a script with a fascinating challenge.

It took me three attempts to understand Paul’s ideas, then suddenly the penny dropped and I saw the argument strategy in all its majesty. The secret is one script but multiple outcomes.  Each result is controlled by a different external argument.

The reason for my struggle was because Paul, through his script, was taking me to places I don’t normally go. It was good for me to outside my comfort zone, it was also good for me to experience role reversal, Paul was teaching me a new concept.

This Week’s Mission

The only way that you will achieve This Week’s Mission is if you pay attention to detail.  Remember that the key concept is one script with many outcomes.  This article will make nosense unless you visit, or at least visualize this place Logon Script dialog box.  (See screen shot).  Directions: Active Directory Users and Computers, Group Policy, User Configuration, Windows, Script Logon, Script name and particularly, Script PARAMETERS. This Edit Script dialog box allows us to specify the Script Name, (something I have done 100+ times), plus Script Parameters, which I had never configured before studying Paul’s script.

Script Parameters, Script Name, VBScript arguments.named

When I first saw Paul’s script I noticed that in the Script Name: dialog box was, cscript.exe.  I threw a John McEnroe tantrum and thought: ‘Paul you cannot be serious’.  However, just like the tennis umpire, Paul was correct and just like John McEnroe, I was wrong.  The name in the top dialog box should be the mother of VBScript – cscript.exe.

All of the work, all of the skill, is in the Script Parameter box, e.g. \\ uncpath\script.vbs "/argument:value". In practical terms, the parameter sets different values for the named argument inside the VBScript. The script (paul.vbs) then reacts according to these rules, if the switch = "/DriveMap:J:", then the value = J: is passed to the DriveMap argument.

Now, if we focus on "/DriveMap:J:" and substitute say "/DriveMap:S:", then perhaps you can see how we achieve our goal of one script, many outcomes. For example, in the next OU, cscript.exe and \\ grand\paul.vbs remains the same, but "/DriveMap:J:", becomes "/DriveMap:S:". The result is the script now maps to a different drive letter, S: instead of J:.

To illustrate these external arguments, I have deliberately chosen the simplest example that I could think of. With your imagination, you could project how to substitute other names for the DriveMap argument, for example, group, department or manager. Your values could be sales, accounts or IT rather than J: or S:.

To get the complete picture of what is happening and to make the argument connection, you need to examine the commands inside the VBScript (paul.vbs). In particular, we need to investigate how VBScript applies, ‘If then…’ logic to DriveMap and its value J:.

Beware the syntax.  Pay close attention to "/DriveMap:J:".  The trap I fell into was to introduce a space between the first colon and the drive letter. Here is my mistake – "/DriveMap: J:". Incidentally, just a batter (batsman) is always looking for the sly curve ball (or Shane Warne’s googly), so I am scanning my scripts for the devious syntax error.

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 1: VBScript Arguments in the Parameter Box

Our mission is to configure external arguments. The purpose of this script is to map a UNC path to a drive letter specified by the external argument "/DriveMap:J:".  This argument (parameter) is controlled via the Group Policy logon script dialog box.

Pre-requisites and preparation.

Most weeks you only need to skim this pre-requisites section, this week I exhort you to read every word – slowly.

To make this work, you need access to Active Directory Group Policy. Moreover, to test the script you must logon as a user whose account is in the OU where you set the Group Policy with its Script Parameters.

You will need Active Directory and also a shared folder for your VBScript. If no other machine is available, share out a folder on your own machine. Make sure that you amend strRemotePath to reflect your UNC path and not mine.

Instructions for using external arguments

Stage 1

  1. To set the Parameters box, navigate to Active Directory then, Group Policy, User Configuration, Windows, Script Logon, Script name and particularly, Script PARAMETERS.  e.g. \\ uncpath\paul.vbs "/DriveMap:J:".

  2. Remember that the Script Name should be cscript.exe (not paul.vbs).

  3. Amend \\ uncpath to reflect the network path where you save the script.

Stage 2

  1. Save the file with a .vbs extension, for example: paul.vbs.  N.B. Save the file to the place referenced by \\ uncpath

  2. Amend strRemotePath.  If necessary create a network share.

  3. Logon as user who is in the OU where you configured the Group Policy.

  4. To help with the Script itself, a good editor such as OnScript will assist by color coding the commands. Get your evaluation copy of OnScript

 

‘ paul.vbs
‘ VBScript which applies external arguments
‘ Author Guy Thomas
‘ Version 2.5 – March 2006
‘ ————————————————‘

Option Explicit
Dim objNetwork
Dim strRemotePath, strDriveLetter

strRemotePath = "\\grand\ezine"

‘ Uncomment the next two lines for troubleshooting
‘ strDriveLetter = "u:"
‘ On Error Resume Next

Set objNetwork = CreateObject("WScript.Network")

‘ The next lines are the heart of the script
‘ The value of DriveMap is set in the Script Parameters in AD.
If WScript.Arguments.Named.Exists("DriveMap") Then
strDriveLetter = WScript.Arguments.Named("DriveMap")
End If

objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
WScript.Quit

‘ End of script.

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

Learning Points

Note 1:  The name and value of the argument ("/DriveMap:J:") are set in Active Directory, Group Policy.

Note 2:  As far as the script is concerned, focus on the If ..then logic.  Examine how the value for strDriveLetter is extracted from WSCript.Arguments.Named("DriveMap").

Note 3:  For troubleshooting, I have included two ‘Remmed out’ lines in the middle of the script.  If you proceed with ("/DriveMap:J:") then when you logon, you should see a mapped J: drive.  If this script does not work remove the apostrophe from – ‘ strDriveLetter = "U:".  At least you should then get a mapped U:.  If nothing happens, then I will bet that you are logging on with an account that is not in the OU where you configured the Group Policy setting.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Summary – External Arguments

The key concept is one VBScript with many outcomes.  To control the scripts configure different external arguments.  Our mission is to combine settings at the Active Directory Logon Script dialog box with commands in the VBScript.

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