How to Use the Network
WSH 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), check out PowerShell, discussed later in this chapter. For more
information on networking, see Chapter 7.
The following routines provide access to some of the more useful networkrelated
functions in VBScript.
532 | Chapter 9: Scripting and Automation
The following function checks a given drive letter to see whether 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.
Pages:
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740