PowerShell Ezine, Logon Scripts

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 no sense 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.


If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


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.

 

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.

ˆ

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.

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.