Flylib.com

Books Software

 
 
 

Creating a Login Script


Creating a Login Script

As mentioned at the beginning of this chapter, Tom has decided to combine the tasks of setting up the network drive and printer connections into a single script. Once the script is finished, he will hand it off to Rick and Sue for implementation using AD Group Policy. Rick and Sue will then set Tom's VBScript up as a login script, thus ensuring that it will execute as part of each user 's login process.

The Initialization Section

This script begins by defining the variables and objects that the script will require to execute. In addition to using Option Explicit , the On Error Resume Next statement has been added. This allows the script to continue processing in the event that a network resource is temporarily unavailable. The script will be written to create its mapped network drive connection before moving on to create its printer connection. The On Error Resume Next statement will allow the script to continue running in the event that the network drive is unavailable. It will also lessen user confusion by preventing the display of error messages during login.

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 assigns values to four variables that specify the drive letter, network drive address, and the network addresses of the company's two network printers. The network printer assigned to the str CopyRoomPrinter variable will be set up as the network printer connection on all computers used by nonmanagers, whereas the strMgmtPrinter variable will be used to set up a network printer connection on all computers used by company managers.

The Main Processing 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 associates .

  • The next three letters of the username are the first letters of the user's first, middle, and last names .

  • {% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}

    The last two characters of the username are a number used to differentiate between two users with the same initials .

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

The DisplayNetworkData() subroutine displays information about the user's network connection, as shown below. This information displayed inside the Windows console will briefly appear on the user's desktop while the login script executes.

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

The MapNetworkDrive() Subroutine

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

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

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 Fully Assembled Script

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.

click to expand
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