Mapping a Network Drive and Starting an Application
The following WSH script, written in VBScript, shows how to use
a WshNetwork object to map a network drive, doing so only when
it is Administrator that is logged in. The script also uses the
Run method of WshShell to start the Excel application on a
particular spreadsheet file. This could be expanded to start
Word or other applications on commonly used files, as a quick way
to prepare for the day's work.
' Filename: init.vbs
'
' Author: Br. David Carlson
'
' Date: July 29, 2001
'
' This WSH script checks to see if it is Administrator who
' is logged in. If so, the script maps a network drive and
' open Excel on a particular file on the mapped drive.
Dim net
Dim shell
Dim quote
Dim pgm
Dim fname
set net = Wscript.CreateObject("Wscript.Network")
if net.UserName = "Administrator" then
net.MapNetworkDrive "K:", "\\cisdept\users\smithj", , "smithj", "*password*"
set shell = WScript.CreateObject("WScript.Shell")
quote = Chr(34)
pgm = "c:\program files\microsoft office\office\excel.exe"
fname = "k:\courses\grant\directortime.xls"
shell.Run quote & pgm & quote & " " & fname
end if
|
The WshNetwork object has properties UserName, UserDomain, and ComputerName.
Here we check to see if UserName matches "Administrator". The MapNetworkDrive
method is then used to set up a mapped network drive named K that corresponds
to "\\cisdept\users\smithj". The parameters to this method are the drive letter,
the remote name, an optional boolean flag that indicates whether to save this
drive mapping in the user's profile (default false), and an optional user name
and password. If the user name and password for the network drive are those
of the logged in user, these two parameters can be omitted.
This script also creates a WshShell object and uses its Run method to start
Excel. Note the use of Chr(34) to get a double quote character. The &
concatenation operator is used to build up the needed parameter for shell.Run.
The space character in the pathname is the reason that an extra set of quotes
is needed around this pathname. An equivalent method would be to use:
shell.Run """c:\program files\microsoft office\office\excel.exe"" k:\courses\grant\directortime.xls"
In a similar way, the following program starts up Word on two particular files,
c:\init.vbs and c:\hello.vbs.
This script does not map any network drives; it simply starts Word. Two double
quotes in a row are used to enclose the path to Word. This is needed because
the space in "program files" would otherwise cause us to attempt to run
"c:\program" on the parameters that follow. The Chr(34) method could be
used instead.
' Filename: init2.vbs
'
' Author: Br. David Carlson
'
' Date: August 3, 2001
'
' This WSH script starts up word on the 2 particular files named below.
Dim shell
set shell = WScript.CreateObject("WScript.Shell")
shell.Run """c:\program files\microsoft office\office\winword.exe"" c:\init.vbs c:\hello.vbs"
|
The following is a variation on the init.vbs script. This one just displays a message and
quits if you are logged in as Administrator. Otherwise, the script opens Excel on a particular
spreadsheet in a particular location. Obviously, if your pathname to Excel.exe is different
or if you do not have a spreadsheet by the name given below at the indicated location, you
will get an error message. This script does not do any mapping of a network drive. Rather, it assumes
that the M drive is the correct one to use.
' Filename: init3.vbs
'
' Author: Br. David Carlson
'
' Date: February 11, 2007
'
' This WSH script checks to see if it is Administrator who
' is logged in. If so, the script prints a message and quits.
' Otherwise, the script opens Excel on a particular file.
' If the file does not exist you will get an error message.
Dim net
Dim shell
Dim quote
Dim pgm
Dim fname
set net = Wscript.CreateObject("Wscript.Network")
if net.UserName = "Administrator" then
Msgbox "Safer not to run scripts and programs as administrator unless really needed"
else
set shell = WScript.CreateObject("WScript.Shell")
quote = Chr(34)
pgm = "c:\program files\microsoft office\office11\excel.exe"
fname = "M:\example.xls"
shell.Run quote & pgm & quote & " " & fname
end if
|
The following is a variation on the init2.vbs script.
It shows you how your might open Word on two documents, when a space occurs in the path to the documents.
This complication is handled by quoting.
' Filename: init4.vbs
'
' Author: Br. David Carlson
'
' Date: February 11, 2007
'
' This WSH script starts up word on the 2 particular files named below.
Dim shell
Dim quote
Dim pgm
Dim fname1
Dim fname2
set shell = WScript.CreateObject("WScript.Shell")
quote = Chr(34)
pgm = "c:\program files\microsoft office\office11\winword.exe"
fname1 = "I:\WSH Examples\hello.vbs"
fname2 = "I:\WSH Examples\init.vbs"
shell.Run quote & pgm & quote & " " & quote & fname1 & quote & " " & quote & fname2 & quote
|
|