Collecting Remote Summary Reports

In this chapter, you will learn how to write VBScripts that can connect to network drives and then remotely administer files by copying, moving, and deleting them. Using this information, you will learn how to develop a script that collects the summary reports from the remote Windows 2000 servers at Intuit. As part of the development of this script, you will get the opportunity to work further with the Windows registry by retrieving the script's configuration settings.

Prerequisite Tasks

Before beginning work on her new project, Molly has a number of small tasks that she needs to accomplish. These tasks include:

Creating a System Account

The first of the prerequisite tasks that Molly works on is the creation of a domain level system account. She arranged to have this account set up so that its password never changes. By setting this account up at the domain level, Molly ensures that it will have the security rights and permissions that are required to run her scripts on both the Windows 2000 servers and the Windows 2000 Professional workstation.

The name of the account that she created is ScriptSchd. Molly will associate this account with a scheduled task that she plans to set up on the Windows 2000 Professional workstation in order to set up the automated execution of her scheduling script.

Creating the Scheduler Script

Molly next creates the scheduling script that will manage the daily and monthly execution of the following VBScripts:

Molly created this new script, shown below, by copying and modifying the scheduling script that she developed for the previous project.

'************************************************************************* 'Script Name: Script 23.1.vbs 'Author: Jerry Ford 'Created: 04/10/03 'Description: This script runs the following list of scripts: ' The Remote summary report collection script ' The Report consolidation script ' The Archive management script '************************************************************************* 'Initialization Section Option Explicit On Error Resume Next Dim WshShl Set WshShl = WScript.CreateObject("WScript.Shell") 'Main Processing Section RunScript("SumRptRetrieve.vbs") RunScript("RptConsolidator.vbs") RunScript("ArchiveMgr.vbs") If Day(date()) = 1 Then RunScript("ArchiveMgr.vbs") End If 'Terminate script execution WScript.Quit() 'Procedure Section Sub RunScript(ScriptName) WshShl.Run ScriptName, 1, True End Sub Sub WriteToEventLog() WshShl.LogEvent 4, "Master Scheduler Script executing." End Sub

Creating the Network Folders

Next Molly visits both of the Windows 2000 servers and sets up the d:Order_InventorySumReports folder as a shared folder. She grants the ScriptSchd account access to this new network folder, as outlined in the following procedure.

  1. After logging on to a Windows 2000 server, Molly double-clicks on the My Computer icon. The My Computer dialog box opens.
  2. She then double-clicks on the D: drive, followed by the Order_Inventory folder. The Order_Inventory folder opens, showing its contents.
  3. Molly right-clicks on the SummaryRpts folder and selects the Sharing option from the menu that appears. This opens the folder's Properties dialog box.
  4. She then selects the Share this folder option and enters SumRpts in the Share name field, as shown in Figure 23.1.

    Figure 23.1: Creating a network share for the SummaryRpts folder located on each of the Windows 2000 servers

  5. Next she clicks on the Permissions button. This displays the Permissions for SummaryRpts dialog box. From here, she can specify which users have remote access to the shared folder. She begins by selecting all currently defined accounts, one at a time, and clicking on Remove. She then clicks on Add and selects the ScriptSchd account from the list of domain accounts that are displayed.
  6. Molly then clicks on OK three times to close all open dialog boxes.

At this point, the SummaryRpts folder is set up as a shared network folder to which only the ScriptSchd domain account has remote access.

  Note

Molly could have also restricted both local and remote access to the SummaryRpts folder by configuring NTFS file permissions instead of or in addition to share level permissions.

Setting Up Connections to Network Drives and Folders

The basic purpose of the report retrieval script is to retrieve the summary report files from the two Windows 2000 servers. In order to perform this task, the script must have access to the folders where the reports reside on each server. This means that the script must have a network connection to these folders from the Windows 2000 Professional workstation where it will execute.

Unfortunately, network drive connections are associated with individual user profiles and are therefore only available when a user with a defined network drive or folder connection is currently logged on to the computer. Because Molly plans to execute the summary report retrieval script using the Windows Task Scheduler service at a time when no users are logged on to the computer, the script must take responsibility for establishing its own network connections to the shared folders on each Windows 2000 server.

Manually Setting Up Connections

The traditional way to connect to a remote network drive or folder is to create a drive mapping to it. A mapped drive is a logical network connection that makes a shared drive or folder look and act as if it were locally connected to the computer where the mapped connection is established.

  Note

In order to map to a network drive or folder, you must be granted the appropriate level of access rights and permissions on the computer where the shared drive or folder exists.

The same steps are followed to manually set up either connection to a network drive or folder and can be performed from any number of Windows folders, including the Windows Explorer and My Computer folders. For example, the following procedure outlines the steps involved in creating a mapping to a remote folder called SumReports on a computer named SERV0002.

  1. Click on Start and then My Computer. The My Computer folder appears.
  2. Click on Tools and select the Map Network Drive menu option. The Map Network Drive dialog box appears.
  3. Select a available drive letter from the Drive drop-down list.
  4. Type \SERV0002SumReports in the Folder field, as demonstrated in Figure 23.2.
  5. To set up a persistent drive mapping that will be restored the next time you log on, make sure that the Reconnect at logon option is selected. Otherwise, clear this option.

    Figure 23.2: Manually creating a mapped connection to a network folder

  6. Click on Finish. The Map Network Drive dialog box closes and is replaced by an Explorer window showing the contents of the network folder, as demonstrated in Figure 23.3.

Figure 23.3: Examining the contents of the mapped folder

Once the mapped connection is established to a network drive or folder, you may work with it like any other local disk drive (provided you have the appropriate security access permissions).

Working with the WshNetwork Object

Because the report collection script will be run by the Task Scheduler service, Molly needs to equip it with the ability to establish its own network connections to each of the Windows 2000 servers' shared folders. To accomplish this, Molly will need to use methods associated with the WshNetwork object.

The WshNetwork object exposes a number of network-related resources on Microsoft networks. It provides access to several properties that can be used to collect network information. These properties are listed below.

In addition to these methods, the WshNetwork object makes available a number of methods that provide the ability to work with network disk and printer resources. These methods include:

  Note

To learn more about the properties and objects associated with the WshNetwork object, refer to Chapter 14, "Mapping Network Printers and Disks."

Mapping Drive Connections

In order to automate the establishment of a mapping to a network drive or folder, Molly needs to learn how to work with the WshNetwork object's MapNetworkDrive() method. The syntax of this method is outlined in Chapter 14. Once executed, the MapNetworkDrive() method sets up a network drive connection that allows a script to move, copy, create, and delete files and folders on the network drive or folder represented by the connection.

The following VBScript statements demonstrate how to establish a temporary connection to a network drive or folder.

Dim WshNtk Set WshNtk = WScript.CreateObject("WScript.Network") WshNtk.MapNetworkDrive "z:", "\SERV0002D"

First an instance of the WshNetwork object is set up. Then its MapNetworkDrive() method is executed, and it is passed a drive letter to be used in creating the mapping and the UNC path of the network drive or folder.

Disconnecting Drive Connections

For good form, Molly wants her script to explicitly disconnect its temporary network drive connections. To do this, she will need to use the WshNetwork object's RemoveNetworkDrive() method. The syntax of this method is outlined in Chapter 14.

An example of how to disconnect the drive mapping set up in the previous example is shown on the following page.

Dim WshNtk Set WshNtk = WScript.CreateObject("WScript.Network") WshNtk.RemoveNetworkDrive "z:"

As you can see, only the drive letter representing the network connection is required.

File Management

Administering files, locally or over a network, involves copying, moving, and deleting them. The WSH provides VBScript with the ability to administer files using methods belonging to the FileSystemObject object. The FileSystem Object object exposes the Windows file system, allowing your scripts to directly interact with and manage it. The FileSystemObject object also provides access to the File object, which provides additional methods for copying, moving, and deleting individual files.

Working with the FileSystemObject Object

In order to work with the FileSystemObject object, you must first instantiate it, as demonstrated below.

Set FsoObj = New WScript.CreateObject ("Scripting.FileSystemObject")

The FileSystemObject object provides direct access to methods that you can use to administer files. Some of these methods are listed below.

Each of these methods allows you to manipulate one or more files at a time. They can be used to administer files on any drive to which the computer has a connection (provided that the appropriate level of security access is available). For more information about these methods, refer to Chapter 14.

Working with the File Object

In addition to the methods provided by the FileSystemObject object, you may use a number of methods provided by the File object when administering files, as listed below.

One major difference between these three methods and the methods provided by the FileSystemObject object are that the File object's methods can only be used to work with one file at a time. Another difference is that the File object's methods can also be used to administer folders, whereas the FileSystemObject object provides an entirely different set of methods for working with folders.

In order to work with the File object, you must first set up an instance of the FileSystemObject object. Once this is done, you can use the FileSystem Object object's GetFile method to set up an instance of the File object, which will represent the file that you want to administer. You can then execute any of the File object's methods.

Of the three File object methods listed above, Molly needs to learn how to work with the Copy() method. Using this method, she plans to copy the summary report from both of the Windows 2000 servers each morning for processing on the Windows 2000 Professional workstation. This method has the following syntax:

ObjectReference.Copy(Target [, Overwrite])

ObjectReference is the variable representing an instance of the File object. Target specifies the destination file or folder that is to be copied. Overwrite is an optional parameter that, when set equal to True, forces an existing file or folder to be overwritten.

The following VBScript statements demonstrate how to use the Copy() method to copy a file called TestFile.txt located in the D:Temp folder to a folder on a mapped network drive.

Set FsoObj = CreateObject("Scripting.FileSystemObject") Set FileName = FsoObj.GetFile("d: empTestFile.txt") FileName.Copy "z:TempTestFile.txt "

Developing the Report Collection Script

Molly is now ready to begin writing the summary report collection script. This script will be responsible for retrieving and storing local copies of the summary reports generated each morning on the Windows 2000 servers where the order/inventory system resides. This script will be executed by the scheduling script and will be immediately followed by a script that processes the reports that it retrieves.

The Initialization Section

Molly begins the script, like all her other scripts, with the Option Explicit statement in order to enforce the strict interpretation of variable names. Next, variables used throughout the script are defined, and the WshNetwork, FileSystemObject, and WshShell objects are instantiated. Finally, a constant is defined that will be used in all pop-up dialog boxes displayed by the script.

Option Explicit Dim strEventLog, strDebug, strSvrList, strFolderList, strArchive Dim WshNtk, FsoObj, WshShl, strSumRptFileName Set WshNtk = WScript.CreateObject("WScript.Network") Set FsoObj = CreateObject("Scripting.FileSystemObject") Set WshShl = WScript.CreateObject("WScript.Shell") Const cTitleBarMsg = "Summary Report Collection Script"

The Main Processing Section

In the Main Processing Section, shown on the following page, a series of subroutine and function calls control the overall execution of the script. The SetDefaultSettings() subroutine sets default configuration settings for the script. Then the GetRegistrySettings() subroutine retrieves script settings stored in the Windows registry, overriding matching default script settings. Next an If statement determines whether logging is enabled and writes a message to the Windows application event log if appropriate.

The MapNetworkDrive() function is executed twice, once for each Windows 2000 server. A drive letter and a UNC path is passed to the function each time. The UNC path is created by prepending the \ characters to the name of one of the Windows 2000 servers, which is extracted from the variable strSvrList using either the Left() or Right() function, the backslash () character, and the name of the shared network folder on the Windows 2000 server (strFolder List).

SetDefaultSettings() GetRegistrySettings() If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script now executing.") End If MapNetworkDrive "X:", "\" & Left(strSvrList, 8) & "" & strFolderList MapNetworkDrive "Y:", "\" & Right(strSvrList, 8) & "" & strFolderList strSumRptFileName = GetSummaryRptFileName() CopyFolders "X:" & strSumRptFileName, Left(strSvrList, 8) CopyFolders "Y:" & strSumRptFileName, Right(strSvrList, 8) DisconnectNetworkDrive("X:") DisconnectNetworkDrive("Y:") If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script finished executing.") End If TerminateScript()

The next statement in the Main Processing Section calls the GetSummary RptFileName() function, which returns the name of the summary report to be retrieved. Then the CopyFolders() subroutine is called twice and passed the name of the file to be retrieved and the name of the server where the file resides.

Once both summary report files have been retrieved, the DisconnectNetworkDrive() subroutine is executed twice in order to disconnect the previously established connections to the Windows 2000 server's shared network folders. If appropriate, another message is written to the Windows application event log and the TerminateScript() subroutine is executed in order to stop the scripts execution.

The SetDefaultSettings() Subroutine

The SetDefaultSettings() subroutine, shown below, defines default configuration settings for the script. These settings will be used to control the script's operation in the event that there is a problem accessing corresponding registry values.

Sub SetDefaultSettings() strEventLog = "Enabled" strDebug = "Disabled" strSvrList = "SERV0001 SERV0002" strFolderList = "SumReports" strArchive = "d:Order_InventroryLogFiles" If strDebug = "Enabled" Then MsgBox "Default settings initialized: " & vbCrLf & vbCrLf & _ "strEventLog" & vbTab & "=" & vbTab & strEventLog & vbCrLf & _ "strDebug" & vbTab & vbTab & "=" & vbTab & strDebug & vbCrLf & _ "strSvrList" & vbTab & vbTab & "=" & vbTab & strSvrList & vbCrLf & _ "strFolderList" & vbTab & "=" & vbTab & strFolderList & vbCrLf & _ "strArchive" & vbTab & "=" & vbTab & strArchive, , cTitleBarMsg End If End Sub

If the script is being executed in debug mode (that is, the value of strDebug is equal to Enabled) then the VBScript MsgBox() function is used to display the value of each variable.

The GetRegistrySettings() Subroutine

The GetRegistrySettings() subroutine, shown on the next page, begins by executing the On Error Resume Next statement in order to prevent a problem in retrieving a configuration setting from the Windows registry from halting the script's execution. This will allow the script to proceed using a default setting if need be.

Next the subroutine attempts to read a registry value where one of the script's configuration settings is stored. The value of Err.Number (the default property of the Err object) is examined to determine whether an error occurred. If an error did occur and logging is enabled, a message is written to the Windows application event log and the value of Err.Number is reset to zero to clear out the error before the next registry value is read. The above process repeats itself until all the script's registry values have been processed.

Sub GetRegistrySettings() On Error Resume Next strEventLog = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsEventLogging") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strEventLog.") Err.Number = 0 End If End If strDebug = WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsDebugMode") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strDebug.") Err.Number = 0 End If End If strSvrList = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsWin2000Svrs") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strSvrList.") Err.Number = 0 End If End If strFolderList = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsSharedFolder") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strFolderList.") Err.Number = 0 End If End If strArchive = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsRptArchive") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strArchive.") Err.Number = 0 End If End If If strDebug = "Enabled" Then MsgBox "Registry settings initialized: " & vbCrLf & vbCrLf & _ "strEventLog" & vbTab & "=" & vbTab & strEventLog & vbCrLf & _ "strDebug" & vbTab & vbTab & "=" & vbTab & strDebug & vbCrLf & _ "strSvrList" & vbTab & vbTab & "=" & vbTab & strSvrList & vbCrLf & _ "strFolderList" & vbTab & "=" & vbTab & strFolderList & vbCrLf & _ "strArchive" & vbTab & "=" & vbTab & strArchive, , cTitleBarMsg End If End Sub

Finally, if the script is being executed in debug mode, then the subroutine uses the VBScript MsgBox() function to display the value of each variable. Displaying variable values a second time assists Molly in tracking their contents as she develops the script. While she is developing and testing the script, she configures this value of strDebug to equal Enabled. Later, when she is done testing the script, she will change the value of this variable in the Windows registry to Disabled.

The MapNetworkDrive() Function

The MapNetworkDrive() function, shown below, accepts two arguments. The first argument is the drive letter to be used in setting up a network connection, and the second argument is the UNC path of one of the shared network folders residing on the Windows 2000 servers. Numerous intermediate results are displayed in pop-up dialog boxes as the function executes (if the script is run in debug mode).

The FileSystemObject object's FileExists() method is used to determine whether or not the network folder, specified as strDrive, is accessible (the network is not down, the folder exists, and so on). If the folder is accessible, another test is performed to determine whether or not the specified drive letter (strLetter) is in use. If it is then its connection is deleted in order to free up the drive letter for the script. However, if the network folder cannot be accessed when first tested, the TerminateScript() subroutine is called and the script terminates its execution.

Function MapNetworkDrive(strLetter, strDrive) If strDebug = "Enabled" Then MsgBox "strLetter = " & strLetter & vbCrLf & "strDrive = " & _ strDrive, , cTitleBarMsg End If If FsoObj.DriveExists(strDrive) Then If strDebug = "Enabled" Then MsgBox strDrive & " exists", , cTitleBarMsg End If If FsoObj.DriveExists(strLetter) Then If strDebug = "Enabled" Then MsgBox "Deleting drive letter " & strLetter, , cTitleBarMsg End If WshNtk.RemoveNetworkDrive strLetter End If WshNtk.MapNetworkDrive strLetter, strDrive Else If strDebug = "Enabled" Then MsgBox strDrive & " does not exist", , cTitleBarMsg End If If strEventLog = "Enabled" Then WriteToEventLog "Summary Report Collection script - Unable to map " & _ "to network drive " & strDrive End If TerminateScript() End If End Function

Finally, the WshNetwork object's MapNetworkDrive() method is executed and passed a drive letter and the UNC path to a shared network folder.

The GetSummaryRptFileName() Function

The GetSummaryRptFileName() function, shown on the next page, determines the name of the current summary reports. It does so by completing a number of steps. First, the Date() function to get the current system date. Then the Replace() function is used to substitute all instances of the backslash (/) character, which is specified within the current date with the dash () character. Finally, the string value of _SumRpt.txt is appended to the end of the reformatted system date to arrive at the summary report's file name. This file name is returned to the statement that called this function by assigning its value to a variable that has the same name as the function.

Function GetSummaryRptFileName() GetSummaryRptFileName = Replace(Date(), "/", "-") GetSummaryRptFileName = GetSummaryRptFileName & "_SumRpt.txt" If strDebug = "Enabled" Then MsgBox "Summary Report Files Name = " & GetSummaryRptFileName, , _ cTitleBarMsg End If End Function

The CopyFolders() Subroutine

The CopyFolders() subroutine, shown below, begins by determining whether or not the summary report is accessible. If it is not accessible, the script's execution is terminated by calling the TerminateScript() subroutine. If it is accessible, the FileSystemObject object's GetFile() method is used to instantiate an instance of the File object and create an association with a summary report (as specified by strFileNameToCopy).

Sub CopyFolders(strFileNameToCopy, strServerName) Dim strFileName If (FsoObj.FileExists(strFileNameToCopy)) Then If strDebug = "Enabled" Then MsgBox "File " & strFileNameToCopy & " found. Now copying...", , _ cTitleBarMsg End If Else If strDebug = "Enabled" Then MsgBox "File " & strFileNameToCopy & " does not exist. Stopping " & _ "script execution.", , cTitleBarMsg End If TerminateScript() End If Set strFileName = FsoObj.GetFile(strFileNameToCopy) strFileName.Copy (strArchive & "" & strServerName & "_" & _ Mid(strFileNameToCopy,4)) If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog "Summary Report Collection script - Unable to copy " & _ strFileNameToCopy End If If strDebug = "Enabled" Then MsgBox "Summary Report Collection script - Unable to copy " & _ strFileNameToCopy, , cTitleBarMsg End If TerminateScript() End If End Sub

Once instantiated, the File object's Copy() method is used to copy and rename the summary report to a local folder on the Windows 2000 Professional workstation. The value stored in strArchive specifies the folder where the summary report is to be stored. The name of the summary report is changed by appending the backslash () character, the value of strServerName, the underscore (_) character, and the value of strFileNameToCopy (less the drive letter specified in the first three characters of the strFileNameToCopy).

The DisconnectNetworkDrive() Subroutine

Once copies of both summary reports have been copied over to the Windows 2000 Professional workstation, the DisconnectNetworkDrive() subroutine is executed twice. The subroutine begins by executing the On Error Resume Next statement in order to prevent any problem that may occur when disconnecting a drive connection from halting the script's execution. Each time the subroutine is called, a different drive letter is passed to it, specifying the network connection to be disconnected.

Sub DisconnectNetworkDrive(strDriveLetter) On Error Resume Next If strDebug = "Enabled" Then MsgBox "Disconnecting " & strDriveLetter, , cTitleBarMsg End If WshNtk.RemoveNetworkDrive strDriveLetter If Err <> 0 Then If strDebug = "Enabled" Then MsgBox "Error occurred when disconnecting " & strDriveLetter, , _ cTitleBarMsg End If End If End Sub

The WriteToEventLog() Subroutine

The WriteToEventLog() subroutine accepts a single argument representing a message to be written to the Windows 2000 Professional workstation's application event log. It then writes this message as an informational event.

Sub WriteToEventLog(strMessage) WshShl.LogEvent 4, strMessage End Sub

The TerminateScript() Subroutine

The TerminateScript() subroutine, shown below, displays a termination message in a pop-up dialog box, if the script is run in debug mode. It then executes the WScript object's Quit() method to halt the script's execution.

Sub TerminateScript() If strDebug = "Enabled" Then MsgBox "Script execution terminated.", , cTitleBarMsg End If WScript.Quit() End Sub

The Fully Assembled Script

The fully assembled script is shown below. When executed, it will retrieve its configuration settings from the Windows registry, connect to the shared network folders located on each of the Window 2000 servers, and copy the current day's summary reports over to the Windows 2000 Professional workstation.

'************************************************************************* 'Script Name: Script 23.2.vbs 'Author: Jerry Ford 'Created: 04/11/03 'Description: This script copies files from remote servers to a central 'folder on a Windows 2000 Professional workstation. '************************************************************************* 'Initialization Section Option Explicit Dim strEventLog, strDebug, strSvrList, strFolderList, strArchive Dim WshNtk, FsoObj, WshShl, strSumRptFileName Set WshNtk = WScript.CreateObject("WScript.Network") Set FsoObj = CreateObject("Scripting.FileSystemObject") Set WshShl = WScript.CreateObject("WScript.Shell") Const cTitleBarMsg = "Summary Report Collection Script" 'Main Processing Section SetDefaultSettings() GetRegistrySettings() If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script now executing.") End If MapNetworkDrive "X:", "\" & Left(strSvrList, 8) & "" & strFolderList MapNetworkDrive "Y:", "\" & Right(strSvrList, 8) & "" & strFolderList strSumRptFileName = GetSummaryRptFileName() CopyFolders "X:" & strSumRptFileName, Left(strSvrList, 8) CopyFolders "Y:" & strSumRptFileName, Right(strSvrList, 8) DisconnectNetworkDrive("X:") DisconnectNetworkDrive("Y:") If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script finished executing.") End If TerminateScript() 'Procedure Section Sub SetDefaultSettings() strEventLog = "Enabled" strDebug = "Disabled" strSvrList = "SERV0001 SERV0002" strFolderList = "SumReports" strArchive = "d:Order_InventroryLogFiles" If strDebug = "Enabled" Then MsgBox "Default settings initialized: " & vbCrLf & vbCrLf & _ "strEventLog" & vbTab & "=" & vbTab & strEventLog & vbCrLf & _ "strDebug" & vbTab & vbTab & "=" & vbTab & strDebug & vbCrLf & _ "strSvrList" & vbTab & vbTab & "=" & vbTab & strSvrList & vbCrLf & _ "strFolderList" & vbTab & "=" & vbTab & strFolderList & vbCrLf & _ "strArchive" & vbTab & "=" & vbTab & strArchive, , cTitleBarMsg End If End Sub Sub GetRegistrySettings() On Error Resume Next strEventLog = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsEventLogging") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strEventLog.") Err.Number = 0 End If End If strDebug = WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsDebugMode") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strDebug.") Err.Number = 0 End If End If strSvrList = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsWin2000Svrs") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strSvrList.") Err.Number = 0 End If End If strFolderList = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsSharedFolder") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strFolderList.") Err.Number = 0 End If End If strArchive = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsRptArchive") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strArchive.") Err.Number = 0 End If End If If strDebug = "Enabled" Then MsgBox "Registry settings initialized: " & vbCrLf & vbCrLf & _ "strEventLog" & vbTab & "=" & vbTab & strEventLog & vbCrLf & _ "strDebug" & vbTab & vbTab & "=" & vbTab & strDebug & vbCrLf & _ "strSvrList" & vbTab & vbTab & "=" & vbTab & strSvrList & vbCrLf & _ "strFolderList" & vbTab & "=" & vbTab & strFolderList & vbCrLf & _ "strArchive" & vbTab & "=" & vbTab & strArchive, , cTitleBarMsg End If End Sub Function MapNetworkDrive(strLetter, strDrive) If strDebug = "Enabled" Then MsgBox "strLetter = " & strLetter & vbCrLf & "strDrive = " & _ strDrive, , cTitleBarMsg End If If FsoObj.DriveExists(strDrive) Then If strDebug = "Enabled" Then MsgBox strDrive & " exists", , cTitleBarMsg End If If FsoObj.DriveExists(strLetter) Then If strDebug = "Enabled" Then MsgBox "Deleting drive letter " & strLetter, , cTitleBarMsg End If WshNtk.RemoveNetworkDrive strLetter End If WshNtk.MapNetworkDrive strLetter, strDrive Else If strDebug = "Enabled" Then MsgBox strDrive & " does not exist", , cTitleBarMsg End If If strEventLog = "Enabled" Then WriteToEventLog "Summary Report Collection script - Unable to map " & _ "to network drive " & strDrive End If TerminateScript() End If End Function Function GetSummaryRptFileName() GetSummaryRptFileName = Replace(Date(), "/", "-") GetSummaryRptFileName = GetSummaryRptFileName & "_SumRpt.txt" If strDebug = "Enabled" Then MsgBox "Summary Report Files Name = " & GetSummaryRptFileName, , _ cTitleBarMsg End If End Function Sub CopyFolders(strFileNameToCopy, strServerName) Dim strFileName If (FsoObj.FileExists(strFileNameToCopy)) Then If strDebug = "Enabled" Then MsgBox "File " & strFileNameToCopy & " found. Now copying...", , _ cTitleBarMsg End If Else If strDebug = "Enabled" Then MsgBox "File " & strFileNameToCopy & " does not exist. Stopping " & _ "script execution.", , cTitleBarMsg End If TerminateScript() End If Set strFileName = FsoObj.GetFile(strFileNameToCopy) strFileName.Copy (strArchive & "" & strServerName & "_" & _ Mid(strFileNameToCopy,4)) If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog "Summary Report Collection script - Unable to copy " & _ strFileNameToCopy End If If strDebug = "Enabled" Then MsgBox "Summary Report Collection script - Unable to copy " & _ strFileNameToCopy, , cTitleBarMsg End If TerminateScript() End If End Sub Sub DisconnectNetworkDrive(strDriveLetter) On Error Resume Next If strDebug = "Enabled" Then MsgBox "Disconnecting " & strDriveLetter, , cTitleBarMsg End If WshNtk.RemoveNetworkDrive strDriveLetter If Err <> 0 Then If strDebug = "Enabled" Then MsgBox "Error occurred when disconnecting " & strDriveLetter, , _ cTitleBarMsg End If End If End Sub Sub WriteToEventLog(strMessage) WshShl.LogEvent 4, strMessage End Sub Sub TerminateScript() If strDebug = "Enabled" Then MsgBox "Script execution terminated.", , cTitleBarMsg End If WScript.Quit() End Sub

Figure 23.4 shows the summary report files once they have been copied over from each Windows 2000 server for the first time. They are stored on the Windows 2000 Professional workstation in D:Order_InventoryLogFiles. As you can see, each of the summary report file names has been modified to include the name of the server from which it was collected.

Figure 23.4: Examining the summary report files after they have been copied over to the Windows 2000 Professional workstation

Summary

This chapter showed you how to create VBScripts that can establish remote connections to shared network drives. You also learned how to copy, move, and delete network files. You observed as Molly developed a VBScript that collected remotely stored summary reports and stored them on the Windows 2000 Professional workstation for later processing. In the process, you strengthened your understanding of how to retrieve script settings stored in the Windows registry.

Категории