Guy’s Scripting Ezine 105 – VBScript Arguments

Guy’s Scripting Ezine 105 – VBScript Arguments

 ♣

This Week’s Secret

In everyday life I try to avoid arguments.  It’s much the same with VBScript, I only look for extra arguments as a last resort.

Regarding logon scripts, I would like to repeat my views, which I outlined in last Wednesday’s Best Practice ezine.  My position is that I love logon scripts, they are my best selling ebook.  Moreover, I am happy to help people troubleshoot their logon scripts.

The big ‘however’, is that I wonder whether Logon Scripts have a long-term future (6-10 years).  It is my belief that one day a Group Policy will do everything that a logon script can do today.  Therefore my advice in 2006 is this, check to see if there is Group Policy to achieve what you want e.g. Folder Direction for the home drive, if not then by all means use a logon script.

My final word on this debate is that if you deploy logon scripts, make sure that you apply the scripts via Group Policy and not via the user’s property sheet.

This Week’s Mission

Last week we created scripts using the FileSystemObject, this week we are going to use the Network object.  Our mission is to investigate the MapNetworkDrive method and in due course, deploy all five of its arguments.  Incidentally, VBScript’s methods are not case sensitive, the capitalization is merely a way of making its meaning easier to decipher.

Example 1  MapNetworkDrive

This example is designed as both an introduction for beginners and as a refresher for the more experienced scripters.  This example only employs the two commonest arguments, drive letter and UNC path.

Example 2  MapNetworkDrive – Arguments

Example 2 features the thrust of this week’s ezine, to illustrate that VBScript methods often have extra arguments.  In this example we employ all five MapNetworkDrive arguments.

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: To Map a Network Drive to letter U:

Pre-requisites

You need a free local drive letter.  Rather eccentrically, I have chosen letter U as the drive letter because my H: is already in use.  Feel free to amend strDriveLetter.

You will need a shared folder.  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.

This script should work on any Windows, machine.  However, Example 2 will need active directory for the username.

Instructions for mapping network drive

  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 it displays each method’s arguments.

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

  4. Double click mapnetworkdrive.vbs, the check the drive letters with Windows Explorer.

‘ MapNetworkDrive.vbs
‘ VBScript to map a network drive.
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 1.2 – March 2006
‘ ———————————————————————-‘
Option Explicit
Dim objNetwork, objExp
Dim strDriveLetter, strRemotePath

‘ Values of variables set
strDriveLetter = "u:"
strRemotePath = "\\grand\ezine"

‘ This section creates a network object. (objNetwork)
‘ Then applies the MapNetworkDrive method. Result U: drive

Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath

‘ Just to check the script has executed.
WScript.Echo "Check explorer for " & strDriveLetter
WScript.Quit

‘ End of Example script .

Learning Points

Note 1:  Example 1 illustrates the classic VBScript technique: object, method, value.  This week we feature a network object (not a FileSystemObject).

Note 2:  In a production script we would remove the WScript.echo command.  In testing, I like such commands because they demonstrate that the script has completed.

Note 3:  This script, while great for learning the MapNetworkDrive basics, does have deficiencies.  For instance, it does not allow you to specify a different user’s credentials.  More irritatingly, it gives an error message if you run the script for a second time.  For now, add :
On Error Resume Next.  Next week will deal properly with this error problem.

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

Example 2: Employ extra arguments for MapNetworkDrive

We really need a real life scenario to explain why you would add these three arguments, strUser, strPassword, bUpdateProfile.  The first reason is security.  Often administrators want to help users to map drives, but they do not want to give them rights to the root of the network share.  The answer is to employ arguments that embed an administrator’s name and password in the script.  To digress, the name does not have to be ‘The Administrator’, if you are going to do this for real, you could create a generic user and only give them permissions on the share you are mapping.

To digress even further, I can hear the security conscious amongst you saying, ‘shouldn’t we encrypt the password’; well I have investigated, but I have not found a way of encrypting the password within VBScript.  If it is possible, then a kind reader will let me know and I will post the method here for all to see.

The other argument is bUpdateProfile.  When I was a newbie, the letter ‘b’ in bUpdateProfile surprised and confused me, then one day I realized that it was ‘b’ for Boolean, meaning it only took two values, true or false.  This relatively trivial argument controls whether – or not, to update the users profile.  Incidentally, the default is false.

Pre-requisites

It is essential to change the strUser and strPassword otherwise this script will not work on your network.

‘ MapNetWorkDriveA5.vbs
‘ VBScript to map a network drive with 5 arguments.
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 1.4 – March 2006
‘ ———————————————————————-‘
Option Explicit
Dim objNetwork, objExp
Dim strDriveLetter, strRemotePath, strUser, strPassword, bUpdateProfile

‘ Values of variables set
strDriveLetter = "V:"
strRemotePath = "\\grand\ezine"
strUser = "thomasg"   ‘N.B. Must be valid username
strPassword = "P#ssw0rd"    ‘Must be correct password
bUpdateProfile = "true"

‘ This section creates a network object. (objNetwork)
‘ Then apply MapNetworkDrive method. Result V: drive
‘ Note, this script features 5 arguments on lines 24/25.

Set objNetwork = CreateObject("WScript.Network")

objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _
bUpdateProfile, strUser, strPassword

‘ Calls subroutine to check if drive is already connected
call Explorer

WScript.Quit

‘ Sub routine to launch Windows Explorer
sub Explorer()
set objExp = CreateObject("WScript.Shell")
objExp.Run ("Explorer" & " " & strDriveLetter )

End Sub

‘ End of MapNetworkDrive Example script .

Learning Points

Note 0:  When you use a script editor such as OnScript, it helps you by displaying a Method’s arguments.

Note 1:  This is the core script of this week’s ezine.  Here is where we employ all five of MapNetworkDrive’s arguments.  In particular we enable MapNetworkDrive to execute as thought you entered a different user’s name and password in the manual dialog box.  (For example, Windows Explorer, Tools menu, Map Network Drive).

Note 2:  Observe how we use the underscore (_). 
The reason is because all five arguments won’t fit on one line.

Note 3:  I replaced the WScript.echo with a sub routine which opens Windows Explorer so that you can see the mapped drive.

Note 4: The script still lacks error correcting code, consequently, when you run the script a second time, you get error 80070055. Next week we will employ logic to check if the drive is already in use; as a temporary measure, we could add
‘On Error Resume Next’ and so prevent the annoying error.

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 – MapNetworkDrive Arguments

This week we are dealing with network objects.  Many VBScript methods have well-known argument, but often there are lesser know optional arguments.  The focal point of this ezine is Example 2, here is where we employ all five of the MapNetworkDrive methods to create the Mapped Network Drive.

See more about logon scripts

MapNetworkDrive  • Ezines  • Logon Map Network Drive  • Logon Scripts  • LEM 

Ezine 32 Remove  • Ezine 60 Rename  • PowerShell Logon Scripts  • Ezine 74 Map

Ezine 75 Shares  • Ezine 87 Map drive  • Ezine 105 Map Arguments  

Ezine 115 Map Groups  • Ezine 132 Assign Logon  • Tool Kit  • Free CSV Importer