9.6 Networking with Scripts
VBScript has a few limited networking functions built in
that can be used for mapping network drives and connecting to network
printers. For advanced network functionality (such as communication and
network traffic monitoring), you'll have to look into a different scripting
language. For more information on networking, see Chapter 7.
The following routines provide access to some of the more
useful network-related functions in VBScript.
The following function checks a given drive letter to see
if it has already been mapped. It returns True (-1) if the
drive letter has been mapped, False (0) if it hasn't:
Function AlreadyMapped(DriveLetter)
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set AllDrives = WshNetwork.EnumNetworkDrives( )
If Left(DriveLetter,1) <> ":" then DriveLetter = DriveLetter & ":"
ConnectedFlag = False
For i = 0 To AllDrives.Count - 1 Step 2
If AllDrives.Item(i) = UCase(DriveLetter) Then ConnectedFlag = True
Next
AlreadyMapped = ConnectedFlag
End Function
This subroutine maps a drive letter to any valid remote
path:
Sub MapNetDrive(DriveLetter, RemotePath)
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
WShNetwork.MapNetworkDrive DriveLetter, RemotePath
End Sub
This subroutine maps an unused printer port (e.g., LPT3)
to any valid remote network printer:
Sub MapNetPrinter(Port, RemotePath)
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.AddPrinterConnection Port, RemotePath
End Sub
This subroutine removes the mapping for a previously
mapped drive letter:
Sub UnMapNetDrive(DriveLetter)
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
WShNetwork.RemoveNetworkDrive DriveLetter
End Sub
This subroutine removes the mapping for a previously
mapped network printer:
Sub UnMapNetPrinter(Port)
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.RemovePrinterConnection Port
End Sub
The following script serves as an example for these
subroutines. It's used to map a network drive if it's not already mapped or
to disconnect a currently mapped drive. The previous routines are required.
DriveLetter = "N:"
RemotePath = "\\server\c"
If AlreadyMapped(DriveLetter) then
Call UnMapNetDrive(DriveLetter)
Msgbox "Drive " & DriveLetter & " disconnected."
Else
Call MapNetDrive(DriveLetter, RemotePath)
Msgbox "Drive " & DriveLetter & " connected."
End if
This script requires no user interaction once it has been
executed and displays only a single confirmation message when it's done. The
first two lines contain the drive letter and network path to be mapped
together. Then, the AlreadyMapped function is used to determine if
the drive mapping already exists. The script then maps or disconnects the
drive, depending on what's needed. |