Guy's Scripting Ezine 105 - VBScript Arguments
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.
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.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
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 Copy and paste the example script below
into notepad or use a VBScript editor.
One advantage of a good script editor such as OnScript is that it displays each method's arguments.
Save the file with a .vbs extension, for example: mapnetworkdrive.vbs
Double click mapnetworkdrive.vbs, the check the drive letters with Windows Explorer.
' MapNetworkDrive.vbs ' VBScript to map a network drive. ' Author Guy Thomas http://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.
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 http://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.
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.
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.
|