Enterprise Networking BY JOHN E. JOHNSTON A Windows Scripting Line Utility Example

n the March 2000 issue of Technical FIGURE 1: SAMPLE COMMAND LINE UTILITY SCRIPT TO I Support, I wrote an introductory column FILES FROM A NETWARE SERVER TO A WINDOWS NT SERVER on Windows Scripting. Since its publica- tion, several readers have emailed me, asking ' Command Line Utility Script for a command line Windows Scripting ' example. This of utility script can be ' Create the Network Object used to automate a variety of functions such ' Dim WSHNetwork as re-booting file servers, starting a backup Set WSHNetwork = WScript.CreateObject("WScript.Network") job or monitoring disk space usage. One of ' Map a drive for the NetWare server the most common requests I received was ' for a sample utility that would copy files from one file server to another. This month, Dim Drive1 Dim Share1 I will review a sample Visual Basic script Drive1 = "J:" that copies all of the files from a directory Share1 = "\\nw51\" WSHNetwork.MapNetworkDrive Drive1, Share1 residing on a NetWare server to a backup directory residing on a Windows NT server. 'Map a drive for the Windows NT server The functions performed in the script are ' as follows: Dim Drive2 Dim Share2 Drive2 = "K:" maps network drives to the Novell and Share2 = "\\tsg1_pdc\backup" Windows NT server WSHNetwork.MapNetworkDrive Drive2, Share2 performs a DOS command to 'Create the Shell object and execute the xcopy command copy the data ' removes the network drive mappings Dim WSHShell created previously set WSHShell = Wscript.CreateObject("Wscript.Shell") iRetCode = WSHShell.Run("command.com /k xcopy j:\data\*.* k:", 1, TRUE)

The entire script is illustrated in Figure 1. ' Delete the Network drive mappings created earlier Let's take a closer look each function per- ' formed in this script. WSHNetwork.RemoveNetworkDrive Drive1 WSHNetwork.RemoveNetworkDrive Drive2 Mapping Network Drives The first section of the script creates the ' Clean up and Network object, which is required when ' issuing any type of network functions Wscript.Quit within a script. The first two lines of the script create this Network object. The first pointer to perform our drive mappings and the data we are planning to copy to the line of the script — Dim WSHNetwork — drive mapping deletes. Windows NT server. is simply declaring a variable named The next few lines of code create the The next line calls the MapNetworkDrive WSHNetwork. The second line calls the Drive1 and Share1 variables and then function, passing the Drive1 and Share1 Wscript. CreateObject function to create equate a value to these variables. Notice parameters. The MapNetworkDrive func- the Network object. A pointer to the that the Share1 variable is set to tion in this section of code performs the Network object is then placed in our \\nw51\sys. This is the NetWare file server same function as the following command to WSHNetwork variable. We will use this and the NetWare name that contain map a network drive:

TECHNICAL SUPPORT • MAY 2000 ©2000 Technical Enterprises, Inc. Reproduction of this document without permission is prohibited. net use J: \\nw51\sys FIGURE 2: SAMPLE CODE SEGMENT TO AUTOMATE THE DRIVE MAPPING PROCESS Note: The MapNetworkDrive function, when utilized for a NetWare drive mapping, Function TryMapDrive(intDrive, strShare) Dim strDrive can only map the drive to the NetWare - strDrive = Chr(intDrive + 64) & ":" ume level. In other words, we can't map the On Error Resume Next WSHNetwork.MapNetworkDrive strDrive, strShare NetWare drive to the directory level. TryMapDrive = Err.Number = 0 End Function The next few lines of code map the K: For intDrive = 26 To 5 Step -1 drive to a Windows NT Server share named If TryMapDrive(intDrive, strShare) Then Exit For \\tsg1_pdc\backup. Next If intDrive <= 5 Then Executing a DOS command MsgBox "Unable to connect to network share. " & _ Now that we have our drives mapped for "There are currently no drive letters available for use. " & _ CRLF & _ the NetWare and the Windows NT servers, "Please disconnect one of your existing network connections " & _ we are ready to issue the DOS xcopy com- "and try this script again. ", _ vbExclamation + vbOkOnly, _ mand to copy the data from the NetWare L_Welcome_MsgBox_Title_Text server to the Windows NT server. The Else first two lines of this section of code strDrive = Chr(intDrive + 64) & ":" MsgBox "Connected " & strShare & " to drive " & strDrive, _ define the WSHShell variable, then the vbInformation + vbOkOnly, _ Wscript.CreateObject is called to create the L_Welcome_MsgBox_Title_Text Wscript.Shell object. This object is required for issuing DOS commands within a script. mand. This would cause trouble in our it a bit. One of the sample scripts that comes Next, we issue the Run function of the script because the next lines of the script with the Script Host download Shell to perform the xcopy command. would delete the network drive mappings (www.microsoft.com/vbscript/scripting.asp) Notice that we are calling command.com to before the xcopy had completed. contains a section of code that automates issue the xcopy command. Let's take a look Deleting the Network Mappings the process of mapping network drives. This at each parameter passed to the Run function: The last section of code calls the code segment, illustrated in Figure 2, RemoveNetworkDrive function to delete attempts to map the Z: drive, and if unsuc- command.com — This calls the the drive mappings created previously. To cessful, it attempts to map the Y: drive, and Windows command processor. execute this sample script, you can either so on until an unused drive letter is found. /k — This causes the DOS window to utilize any of the following methods: Integrating this type of logic in our sample be left open after the script executes. script would make it much robust, as The user must exit from the DOS cscript.exe interface (i.e., C:>cscript.exe it would prevent the need for the hard coded window before the script will end. This c:\ and file name of script file) drive letters. will allow the user to check the output double click on the script file from the Happy scripting! If you have any questions of the xcopy command. If you do not Explorer application or comments on this material, or have sug- want this DOS box to remain open, use create a shortcut to the script file on a gestions for future topics, please feel free to the /c parameter instead. user's desktop email me at [email protected]. xcopy j:\data\*.* k: — This is the actual xcopy command. It will copy all of the or, you could also use a scheduling package, files from the data directory to the such as WINAT, to automatically call this share named BACKUP on the Windows script at a certain every day. This NT server. sample script was written in a very simple "1" — This is the WindowStyle property. manner to show you the power of A "1" causes the DOS window to be dis- Windows scripting. played on the user's screen. A "2" causes the window to minimized. A "3" Note: This sample has absolutely no error causes the window to start maximized. checking built into it. TRUE — This parameter tells the script NaSPA member John E. Johnston is the owner to wait for the command to complete CONCLUSION of a web development company that specializes before continuing with the script. A in e-commerce. John also performs contract value of FALSE would cause the script If this column has peaked your interest in work on Novell, NT and Unix networks. He can to continue with the next line of script Windows scripting, you may want to practice be contacted at [email protected]. code right after issuing the xcopy com- by taking this sample script and modifying

WWW.NASPA.NET TECHNICAL SUPPORT • MAY 2000