As mentioned at the beginning of this chapter, Tom has decided to combine the
This script begins by defining the
Option Explicit On Error Resume Next Dim WshNet, strDriveLetter, strNetworkDrive, strNetworkPrinter, FsoObject Dim strUserName Set WshNet = WScript.CreateObject("WScript.Network") Set FsoObject = CreateObject("Scripting.FileSystemObject") strDriveLetter = "Z:" strNetworkDrive = "\\ABCFileSvr\D" strCopyRoomPrinter = "\\ABCPrintSvr\HPLaserPrinter" strMgmtPrinter = "\\ABCPrintSvr\CanonColorPrinter"
In addition to defining an instance of the
WshNetwork
and
FileSystemObject
objects, the
Initialization Section
The VBScript Main Processing Section consists of a series of subroutine calls, as shown below.
DisplayNetworkData() MapNetworkDrive strDriveLetter, strNetworkDrive strUserName = WshNet.UserName If Left(strUserName, 1) = "A" Then SetupPrinterConnection strCopyRoomPrinter SetDefaultPrinter strCopyRoomPrinter MsgBox "Copy Room Printer connected!" Else SetupPrinterConnection strMgmtPrinter SetDefaultPrinter strMgmtPrinter MsgBox "Mgmt Printer connected!" End If WScript.Quit()
When this script runs, the user will see a command console appear on the desktop. The DisplayNetworkData() subroutine displays information about the user's network connection. Next, the MapNetworkDrive() subroutine is executed. It is responsible for mapping the connection to the company's file server. The UNC address of the network file server is passed to the subroutine for processing.
Then the strUserName variable is assigned the username of the person logging on to the computer. Each user at ABC, Inc. is assigned a username that is created based on the following guidelines:
The first character of the username is an
M
for managers or
A
for other
The next three letters of the username are the first
The last two
If the first character of the username is an A , then a connection is set up for the printer represented by the strCopyRoomPrinter variable. That printer is then set up as the user's default printer. Otherwise a connection is set up to the printer represented by the strMgmtPrinter variable and this printer is made the default printer.
Once both the network drive and printer connections have been established, the script executes the WScript object's Quit() method to cleanly terminate the script's execution.
The
DisplayNetworkData()
subroutine displays information about the user's network connection, as shown below. This information displayed inside the Windows console will
Sub DisplayNetworkData() WScript.Echo "Now configuring network drive and printer connections for:" WScript.Echo "Computer name: " & WshNet.ComputerName WScript.Echo "Domain name: " & WshNet.UserDomain WScript.Echo "Username : " & WshNet.UserName End Sub
The MapNetworkDrive() subroutine processes two input arguments, the drive letter to use when setting up the drive mapping and the UNC of the network drive. It begins by determining whether or not the network drive is available. If it is not then a message is displayed to that effect. If it is available, then a check is made to determine whether or not the drive letter to be used in setting up the network drive connection is already in use. If it is, then its connection is deleted. The new network connection is established.
Sub MapNetworkDrive(strLetter, strDrive) If FsoObject.DriveExists(strDrive) Then If FsoObject.DriveExists(strLetter) Then WshNet.RemoveNetworkDrive strLetter End If WScript.Echo "Mapping drive to " & strDrive & " as drive letter " & _ strLetter WshNet.MapNetworkDrive strLetter, strDrive Else WScript.Echo "Unable to map to network drive " & strDrive & _ ". Resource not available" End If End Sub
The SetupPrinterConnection() subroutine displays a message documenting its execution and then uses the UNC of the printer passed to it as an argument to establish the connection to the network printer.
Sub SetupPrinterConnection(strPrinter) WScript.Echo "Connecting to network printer " & strPrinter WshNet.AddWindowsPrinterConnection strPrinter End Sub
The SetDefaultPrinter() subroutine displays a message documenting its execution and then uses the UNC of the printer passed to it as an argument to set the network printer up as the user's default printer.
Sub SetDefaultPrinter(strPrinter) WScript.Echo "Setting connection to network printer " & strPrinter & _ " as default" WshNet.SetDefaultPrinter strPrinter End Sub
The entire VBScript is assembled below. It will run as a script policy under the control of Active Directory when the user logs in. It will establish a standardized collection of network connections, thus ensuring that all users have the same base set of network connections. Figure 14.7 shows the Windows console that will briefly appear on the user's desktop when the script executes.
Figure 14.7:
The login script displays information about the user's network connection as part of the network connection configuration process
'************************************************************************* 'Script Name: Script 14.1.vbs 'Author: Jerry Ford 'Created: 02/23/03 'Description: This script will be used as a login script at ABC 'Inc to configure network printer and network drive access '************************************************************************* 'Initialization Section Option Explicit On Error Resume Next Dim WshNet, strDriveLetter, strNetworkDrive, strNetworkPrinter, FsoObject Dim strUserName Set WshNet = WScript.CreateObject("WScript.Network") Set FsoObject = CreateObject("Scripting.FileSystemObject") strDriveLetter = "Z:" strNetworkDrive = "\\ABCFileSvr\D" strCopyRoomPrinter = "\\ABCPrintSvr\HPLaserPrinter" strMgmtPrinter = "\\ABCPrintSvr\CanonColorPrinter" 'Main Processing Section DisplayNetworkData() MapNetworkDrive strDriveLetter, strNetworkDrive strUserName = WshNet.UserName If Left(strUserName, 1) = "A" Then SetupPrinterConnection strCopyRoomPrinter SetDefaultPrinter strCopyRoomPrinter Else SetupPrinterConnection strMgmtPrinter SetDefaultPrinter strMgmtPrinter End If WScript.Quit() 'Procedure Section 'Display network information Sub DisplayNetworkData() WScript.Echo "Now configuring network drive and printer connections for:" WScript.Echo "Computer name: " & WshNet.ComputerName WScript.Echo "Domain name: " & WshNet.UserDomain WScript.Echo "User name: " & WshNet.UserName End Sub 'Map a drive to the Corporate network drive Sub MapNetworkDrive(strLetter, strDrive) If FsoObject.DriveExists(strDrive) Then If FsoObject.DriveExists(strLetter) Then WshNet.RemoveNetworkDrive strLetter End If WScript.Echo "Mapping drive to " & strDrive & " as drive letter " & _ strLetter WshNet.MapNetworkDrive strLetter, strDrive Else WScript.Echo "Unable to map to network drive " & strDrive & _ ". Resource not available" End If End Sub 'Set up a connection to the corporate network printer Sub SetupPrinterConnection(strPrinter) WScript.Echo "Connecting to network printer " & strPrinter WshNet.AddWindowsPrinterConnection strPrinter End Sub 'Set up the network printer connection as the user's default printer Sub SetDefaultPrinter(strPrinter) WScript.Echo "Setting connection to network printer " & strPrinter & _ " as default" WshNet.SetDefaultPrinter strPrinter End Sub