Report Distribution and Remote Archive Management

This chapter represents the final script to be developed as part of the order/inventory reporting Web-based project. In this chapter, you will observe as Alexander creates a VBScript that copies HTML and Word files, as well as an updated copy of the Archive.html page, from the Windows 2000 Professional workstation to the corporate Web server. This script will also be used to trigger the remote execution of a small archive maintenance script, which will be started and remotely monitored from the Windows 2000 Professional workstation as it executes on the corporate Web server.

Implementing Remote Archive Management

The final tasks to be completed in the order/inventory reporting Web-based project is copying and moving files from the Windows 2000 Professional workstation to shared folders on the corporate Web server and the monthly maintenance of archive files stored in these folders. As he sat down and thought about how to complete these final two tasks, Alexander came up with three different ways of automating these activities. These options include:

  • Using the Windows scheduler on the Windows 2000 Professional workstation to trigger the execution of a script on the workstation that establishes a network connection and performs archive maintenance
  • Setting up a scheduled task on the corporate Web server that triggers a local script to perform archive maintenance
  • Using the Windows scheduler on the Windows 2000 Professional workstation to copy an archive maintenance script to the Web server and trigger its execution using Remote WSH

Initially, Alexander was leaning toward the second option because it was the least complicated. This option eliminates the need for:

  • Network connectivity
  • The establishment of a remote network drive
  • Workstation availability

However, Molly advised Alexander that he had better consult with the company's Web master before setting up a scheduled WSH VBScript on the corporate Web server. It turned out that Molly was correct. Michael Barns, the company's Web master, did not hesitate to tell Alexander that he was not permitted to use the server's scheduler service. Nor was Alexander allowed to store and run any VBScripts locally on the Web server.

Of the two remaining options, Alexander decided to go with the Remote WSH option because it would allow him to locally execute an archive maintenance script without having to store it on the Web server. Remote WSH provides the additional benefit of allowing monitoring of remotely executed scripts.

Introducing Remote WSH

Remote WSH provides the ability to initiate, monitor, react to, and even terminate a remotely executed script. Remote WSH is a new feature introduced by WSH version 5.6. The following requirements must be met in order to use it:

  • Both the local and remote computers must support WSH version 5.6.
  • Both the local and remote computers must be running Windows NT 4.0 Service Pack 3 or higher.
  • Administrative privileges are required on the remote computer.

In addition to these requirements, Remote WSH must be enabled on the target computer. This is done by adding a value of Remote and assigning it a setting of 1 on the following registry key:

HKCUSoftwareMicrosoftWindows Script HostSettings
  Note

If you are attempting to use Remote WSH to execute a remote script on a computer running Windows XP Professional, you may have to first execute the following command on the workstation.

 WScript -regserver

This command registers the WScript.exe execution host as a remote COM server.

Remote WSH Architecture

Remote WSH consists of several objects. These objects and their associated properties, methods, and events are shown in Figure 32.1.

click to expand
Figure 32.1: The Remote WSH consists of three objects

The topmost of these objects is the WshController object. The WshController object is instantiated as shown below.

Set WshControl = CreateObject("WshController")

The WshController object does not have any properties and only supports one method, CreateScript(). CreateScript() is used to create a WshRemote object (that is, to instantiate the WshRemote object). The syntax of the CreateScript() method is shown below.

ObjectReference.CreateScript(CommandLine,[ComputerName])

ObjectReference represents a variable reference to the WshController object. CommandLine is a string value that specifies the location of the script that is to be run remotely, as well as any switches that need to be included. The path to the script must be specified as it relates to its location from the local computer where the controlling script executes. ComputerName specifies the UNC name of the remote computer where the remote script will execute. If ComputerName is omitted, the remote script will run locally. For example, the following statement can be used to set up a WshRemote object reference called RemoteScript that will copy a script called TestScript.vbs to a computer called SERV0010 and load it into a WSH process. However, the remote script does not begin to execute.

Set RemoteScript = WshControl.CreateScript(TestScript.vbs, SERV0010)

The WshRemote object (RemoteScript) represents the remote script and provides the ability to start, monitor, and terminate the remote script.

  Note

The remote script is stored in memory on the remote computer. It is never written to the remote computer's hard disk drive and is deleted when its execution completes.

WshRemote Methods

The WshRemote object provides access to two methods. The Execute() method is used to trigger the remote execution of the script once it has been copied into memory on the remote computer. This method has the following syntax.

ObjectReference.Execute

ObjectReference specifies the variable reference to the WshRemote object. In order for the previously setup remote script to run on the remote computer, the following statement will have to be executed.

RemoteScript.Execute

The WshRemote object's second method is the Terminate() method. This method provides the ability to terminate a remote script. It has the following syntax.

ObjectReference.Terminate

ObjectReference specifies the variable reference to the WshRemote object. For example, to terminate the script that was set up to remotely execute in the previous example, the controlling script would have to execute the following statement.

RemoteScript.Terminate

WshRemote Events

As remote scripts execute, they can trigger up to three different events, which can be tracked by the controlling script. To set up the controlling script to handle events, you must use the WScript object's ConnectObject() method, which is used to connect an object's events with a function or subroutine that has a specified prefix. Remote WSH event procedures are established by assigning them a name made up of this prefix followed by the underscore character and the event name. The ConnectObject() method has the following syntax.

ObjectReference.ConnectObject(TargetObject, EventPrefix)

ObjectReference represents the WScript object. TargetObject specifies the name of the object to be connected to, and EventPrefix specifies a string value that will serve as the event's prefix. For example, the following statement enables the controlling script to trap events generated by the remote script using a prefix of RemoteScript_.

WScript.ConnectObject RemoteScript, "RemoteScript_"

Table 32.1 lists the three types of events that can be triggered by remote scripts.

Table 32.1: Events Triggered by Remote Scripts

Event

Description

Start

Triggered when the remote script begins executing

End

Triggered when the remote script stops executing

Error

Triggered if the remote script experiences an error

Using the previous example, you could establish an event handler for the remote script's Start event, as shown below.

Function RemoteScript_Start()
.
.
.
End Function

WshRemote Properties

If a remote script experiences an error, the Error event can be used to execute a procedure that processes error information provided by the WshRemoteError object. If an error occurs in a remote script, you can retrieve information about the error using the WshRemote object Error property. This property retrieves the WshRemoteError object, which provides access to a list of properties that provide information about the error. Table 32.2 lists each of the properties associated with the WshRemoteError object.

Table 32.2: Properties Associated with the WshRemote Error Object

Property

Description

Description

A description of error

Number

The numeric error code associated with the error

Line

The line number where the error occurred

Source

The object responsible for reporting the error

SourceText

The line of code that generated the error

Character

The character position in the line of code where the error occurred

  Note

The technology behind the scenes that allows the WshController object to work is DCOM, which is short for Distributed Component Object Model. Using DCOM, the WshController object automatically handles all underlying communications between the controlling script and the remote script.

The WshRemote object's other property is the Status property, which provides the ability to track the status of a remotely executing script. The Status property represents the remote script's state as a numeric value. Table 32.3 lists and explains the different values that may be stored in the Status property.

Table 32.3: Remote Script Execution States

Value

Description

0

The remote script has not started executing yet.

1

The remote script is now executing.

2

The remote script has finished executing.

A Quick Remote WSH Example

In order to make sure that he had a working understanding of Remote WSH, Alexander decided to perform a quick test. First he wrote a VBScript that creates a small log file in the c: emp folder of the computer upon which it is executed. He called this script TestScript.vbs. It is executed as a remote script. Then he created the following controlling script on the Windows 2000 Professional workstation and executed it.

Set wshController = CreateObject("WshController")
Set wshRemote = wshController.CreateScript("TestScript.vbs", "\SERV0010")

WScript.ConnectObject wshRemote, "RemoteScript_"

wshRemote.Execute

Do Until wshRemote.Status = strExecutionComplete
 WScript.Sleep 2000
Loop

Sub RemoteScript_Start()
 MsgBox "Script TestScript.vbs is not executing."
End Sub
Sub RemoteScript_End()
 MsgBox "Script TestScript.vbs is finished."
End Sub

The controlling script begins by instantiating the WshController object. The script then copies a script called TestScript.vbs to a computer called SERV0010 and creates an instance of the WshRemote object called wshRemote in order to interact with it. Next, the ConnectObject() method was used to define an event prefix in order to allow the controlling script to react to events generated by the remote script. Then the remote script was started using the Execute() method. The controller script then began a loop that checks every 2 seconds to see if the remote script has finished executing. Meanwhile, the controlling script's RemoteScript_Start() and RemoteScript_End() subroutines execute as the remote script starts and then finishes its execution.

Remote WSH Limitations

Remote WSH has a number of limitations that you must be aware of before working with it. First of all, it does not support the execution of any statements that generate a GUI interface. In other words, you cannot use the VBScript MsgBox() or InputBox() functions or the WSH Echo() and Popup() methods within scripts that will be remotely executed.

Remote WSH scripts are not able to access shared folders when they execute on the remote computer (using the credentials of the person that started them). In addition, Remote WSH does not provide a built-in mechanism for returning the script's output to the controlling script, leaving the responsibility for figuring out how to do so up to the script developer.


Creating the Final Script

Having reviewed the objects, methods, properties, and events that make up Remote WSH, Alexander is now ready to begin the development of the report distribution and remote archive management process. Alexander will complete this task by developing two scripts. The first script will be responsible for copying and moving files between the Windows 2000 Professional workstation and the Web server and for remotely running a second small archive maintenance script on the first day of each month. The archive maintenance script will manage the storage of a 3-month archive of report and HTML files on the corporate Web server.

The Initialization Section

The first script, referred to as the controlling script, begins by defining global variables, constants, and objects in its Initialization Section, as shown below. A value of zero is assigned to intReturnCode, which sets the script's default return code.

Option Explicit

Dim strEventLog, strDebug, strHTMLFolder, strSharedRptFolder, strWebSvrName
Dim strConSolRptLoc, strSharedHTMLFolder, strWordRpt, strHTMLRpt, intReturnCode
Dim strResults

Const cTitleBarMsg = "Remote Archive Management Script"
Const strExecutionComplete = 2

Dim FsoObj, WshNtk, WshShl

Set FsoObj = CreateObject("Scripting.FileSystemObject")
Set WshNtk = WScript.CreateObject("WScript.Network")
Set WshShl = WScript.CreateObject("WScript.Shell")

intReturnCode = 0

The Main Processing Section

The controlling script's Main Processing Section, shown on the following page, begins by calling the GetRegistrySettings() subroutine in order to retrieve its configuration settings from the Windows registry on the Windows 2000 Professional workstation. If debug mode and event logging are enabled, pop-up messages are displayed and written to the Windows application event log noting the script's execution status. Then the MapNetworkDrive() function is called twice and passed the drive letter and UNC name of the shared folders on the corporate Web server where the Reporting files reside.

Next, the GetFileNames() subroutine is called in order to determine the names of the HTML and Word files, representing the current day's consolidated summary reports that are to be copied over to the Web server. The CopyAndMoveFiles() subroutine then copies the current day's Word file and moves the current day's HTML files over. In addition, an updated copy of the Archive.html page is moved over to the Web server. The DisconnectNetworkDrive() subroutine is then executed twice in order to delete the script's previously established network drive connections.

Next, the TimeToCleanArchive() function is executed in order to determine if it is time to remotely run the monthly archive maintenance script. If it is time, then the WshController object is instantiated. The CreateScript() method is then used to set up a WshRemote object reference and move the archive maintenance script, called RemoteArchiveMgr.vbs, to the Web server. The WScript object's ConnectObject() method is then run in order to allow the controller script to track events generated by the remote script. The WshRemote object's Execute() method is then used to start the remote script's execution. A Do…Until loop is set up that runs until the value of the WshRemote object's Status property is equal to strExecutionComplete (that is, 2). At that time, the TerminateScript() subroutine is called and the script's execution terminates.

GetRegistrySettings()

If strEventLog = "Enabled" Then
 WriteToEventLog("Remote Archive Management Script now executing.")
End If

If strDebug = "Enabled" Then
 MsgBox "Remote Archive Management Script now executing.", , cTitleBarMsg
End If

MapNetworkDrive "W:", "\" & strWebSvrName & "" & strSharedRptFolder
MapNetworkDrive "X:", "\" & strWebSvrName & "" & strSharedHTMLFolder

GetFileNames()

CopyAndMoveFiles()

DisconnectNetworkDrive("W:")
DisconnectNetworkDrive("X:")

If strDebug = "Enabled" Then
 MsgBox "Remote Archive Management Script now completed.", , cTitleBarMsg
End If

If strEventLog = "Enabled" Then
 WriteToEventLog ("Remote Archive Management Script finished executing.")
End If

strResults = TimeToCleanArchive()

If strResults = "Yes" Then

 Set wshController = CreateObject("WshController")
 Set wshRemote = wshController.CreateScript("RemoteArchiveMgr.vbs", "\" & _
 strWebSvrName)

 WScript.ConnectObject wshRemote, "RemoteScript_"

 wshRemote.Execute

 Do Until wshRemote.Status = strExecutionComplete
 WScript.Sleep 2000
 Loop

End If

TerminateScript(intReturnCode)

The GetRegistrySettings() Subroutine

As you have already seen in numerous examples, the GetRegistrySettings() subroutine, shown below, is responsible for retrieving the script's configuration settings from the Windows registry.

Sub GetRegistrySettings()

 On Error Resume Next

 strEventLog = _
 WshShl.RegRead("HKLMSoftwareIntuitVBScriptsWebRptingEventLogging")
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog ("HTML Report Conversion Script - Using default " & _
 "for strEventLog. RC = 4")
 TerminateScript(12)
 End If
 End If

 strDebug = WshShl.RegRead("HKLMSoftwareIntuitVBScriptsWebRptingDebug")
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog ("HTML Report Conversion Script - Using default & _
 "for strDebug. RC = 4")
 TerminateScript(12)
 End If
 End If

 strHTMLFolder = _
 WshShl.RegRead("HKLMSoftwareIntuitVBScriptsWebRptingHTMLFolder")
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog ("HTML Report Conversion Script - Using default " & _
 "for strHTMLFolder. RC = 4")
 TerminateScript(12)
 End If
 End If

 strConSolRptLoc = _
 "WshShl.RegRead("HKLMSoftwareIntuitVBScriptsWebRptingConSolRptLoc")
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog ("HTML Report Conversion Script - Using default " & _
 "for strConSolRptLoc. RC = 4")
 TerminateScript(12)
 End If
 End If

 strSharedRptFolder = _
 "WshShl.RegRead("HKLMSoftwareIntuitVBScriptsWebRptingShare_Rpts")
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog ("HTML Report Conversion Script - Using default " & _
 "for strSharedRptFolder. RC = 4")
 TerminateScript(12)
 End If
 End If

 strSharedHTMLFolder = _
 "WshShl.RegRead("HKLMSoftwareIntuitVBScriptsWebRptingShare_HTML")
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog ("HTML Report Conversion Script - Using default " & _
 "for strSharedHTMLFolder. RC = 4")
 TerminateScript(12)
 End If
 End If

 strWebSvrName = _
 WshShl.RegRead("HKLMSoftwareIntuitVBScriptsWebRptingWebServer")
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog ("HTML Report Conversion Script - Using default " & _
 "for strWebSvrName. RC = 4")
 TerminateScript(12)
 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 & _
 "strHTMLFolder" & vbTab & "=" & vbTab & strHTMLFolder & vbCrLf & _
 "strConSolRptLoc" & vbTab & "=" & vbTab & strConSolRptLoc & vbCrLf & _
 "strSharedRptFolder" & vbTab & "=" & vbTab & strSharedRptFolder & _
 "strSharedHTMLFolder" & vbTab & "=" & vbTab & strSharedHTMLFolder & _
 vbCrLf & "strWebSvrName" & vbTab & "=" & vbTab & strWebSvrName & _
 vbCrLf, ,cTitleBarMsg
 End If

End Sub

The MapNetworkDrive() Function

The MapNetworkDrive() function, shown below, is identical to the like-named function from the previous chapter. It accepts two arguments, a drive letter and the location of a shared network drive or folder, and creates a network drive connection.

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(4)
 End If

End Function

The GetFileNames() Subroutine

The GetFileNames() subroutine, shown below, is responsible for ascertaining the names of the current day's Word and HTML versions of the consolidated summary reports, as has been demonstrated in previous chapters.

Sub GetFileNames()

 strHTMLRpt = Replace(Date(), "/", "-")

 strHTMLRpt = strHTMLRpt & "_ConsolSumRpt.html"

 strWordRpt = Replace(strHTMLRpt, "html", "doc")

 If strDebug = "Enabled" Then
 MsgBox "HTML Summary Report File Name = " & strHTMLRpt & vbCrLf & _
 "Word Summary Report File Name = " & strWordRpt, , cTitleBarMsg
 End If

End Sub

The CopyAndMoveFiles() Subroutine

The CopyAndMoveFiles() subroutine, shown in the following example, performs a series of three checks, using the FileSystemObject object's File Exists() method to verify that the three files that it is to copy or move exist. If any of the files are not found, the TerminateScript() subroutine is called and passed a script return code value of 4. If all three files are found, then the GetFile() method is used to establish a reference to each of the three files, which are then copied or moved to the Web server using either the File object's Move() or Copy() methods. The Err object is checked after each move or copy operation to make sure that it was successful. If an error occurred during any of these operations, the TerminateScript() subroutine is called and passed a script return code of 8.

Sub CopyAndMoveFiles()

 Dim strFileName

 If (FsoObj.FileExists(strConSolRptLoc & "" & strHTMLRpt)) = "False" Then
 If strDebug = "Enabled" Then
 MsgBox "File " & strHTMLRpt & " not found. Stopping " & _
 "script execution.", , cTitleBarMsg
 End If
 If strEventLog = "Enabled" Then
 WriteToEventLog "Remote Archive Management Script failed. Unable " & _
 "find file: " & strHTMLRpt
 End If
 TerminateScript(4)
 End If
 If (FsoObj.FileExists(strConSolRptLoc & "" & strWordRpt)) = "False" Then
 If strDebug = "Enabled" Then
 MsgBox "File " & strWordRpt & " not found. Stopping " & _
 "script execution.", , cTitleBarMsg
 End If
 If strEventLog = "Enabled" Then
 WriteToEventLog "Remote Archive Management Script failed. Unable " & _
 "find file: " & strWordRpt
 End If
 TerminateScript(4)
 End If
 If (FsoObj.FileExists(strHTMLFolder & "" & "Archive.html")) = "False" Then
 If strDebug = "Enabled" Then
 MsgBox "File Archive.html not found. Stopping " & _
 "script execution.", , cTitleBarMsg
 End If
 If strEventLog = "Enabled" Then
 WriteToEventLog "Remote Archive Management Script failed. Unable " & _
 "find file: Archive.html"
 End If
 TerminateScript(4)
 End If

 Set strFileName = FsoObj.GetFile(strConSolRptLoc & "" & strHTMLRpt)
 strFileName.Move "W:"
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog "Remote Archive Management Script failed moving " & _
 strHTMLRpt
 End If
 If strDebug = "Enabled" Then
 MsgBox "Remote Archive Management Script failed moving " & _
 strHTMLRpt, , cTitleBarMsg
 End If
 TerminateScript(8)
 End If

 Set strFileName = FsoObj.GetFile(strConSolRptLoc & "" & strWordRpt)
 strFileName.Copy "W:"
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog "Remote Archive Management Script failed copying " & _
 strWordRpt
 End If
 If strDebug = "Enabled" Then
 MsgBox "Remote Archive Management Script failed moving " & _
 strWordRpt, , cTitleBarMsg
 End If
 TerminateScript(8)
 End If

 Set strFileName = FsoObj.GetFile("X:Archive.html")
 strFileName.Delete

 Set strFileName = FsoObj.GetFile(strHTMLFolder & "" & "Archive.html")
 strFileName.Move "X:"
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog "Remote Archive Management Script failed moving " & _
 "Archive.html"
 End If
 If strDebug = "Enabled" Then
 MsgBox "Remote Archive Management Script failed moving " & _
 "Archive.html", , cTitleBarMsg
 End If
 TerminateScript(8)
 End If

End Sub

The DisconnectNetworkDrive() Subroutine

As you have seen in previous chapters, the DisconnectNetworkDrive() subroutine, shown below, is responsible for disconnecting network drive connections previously set up by the MapNetworkDrive() Function.

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 TimeToCleanArchive() Function

The TimeToCleanArchive() function, shown below, uses the Date() and Day() functions to determine whether the script is being executed on the first day of the month. It sets the value of TimeToCleanArchive equal to Yes if this is the case.

Function TimeToCleanArchive()

 If Day(Date()) = 1 Then
 TimeToCleanArchive = "Yes"
 End If

End Function

The RemoteScript_Start() Subroutine

The RemoteScript_Start() subroutine, shown below, is automatically executed when the remote script begins executing. It displays a notification message if the script is executing in debug mode and writes an informational message to the Windows 2000 Professional workstation's application event log if event logging is enabled.

Sub RemoteScript_Start()

 If strEventLog = "Enabled" Then
 WriteToEventLog("Remote Archive Management Script - started.")
 End If

 If strDebug = "Enabled" Then
 MsgBox "Remote Archive Management Script - started.", , cTitleBarMsg
 End If

End Sub

The RemoteScript_End() Subroutine

The RemoteScript_End() subroutine, shown below, executes when the remote script stops running. It displays a pop-up dialog box and records a message to the application event log, if appropriate.

Sub RemoteScript_End()

 If strEventLog = "Enabled" Then
 WriteToEventLog("Remote Archive Management Script - stopped.")
 End If

 If strDebug = "Enabled" Then
 MsgBox "Remote Archive Management Script - stopped.", , cTitleBarMsg
 End If

End Sub

The RemoteScript_Error() Subroutine

The RemoteScript_Error() subroutine, shown below, executes if the remote script experiences a error. It displays the values stored in WshRemoteError error properties when debug mode is enabled. It also records a message to the application event log if event logging is enabled.

Sub RemoteScript_Error()

 strErrorNo = Hex(wshRemote.Error.Number)
 strErrorNo = CStr(strErrorNo)

 If strEventLog = "Enabled" Then
 WriteToEventLog ("Error Number: " & strErrorNo & vbCrLf & _
 "Line Number: " & wshRemote.Error.Line & vbCrLf & _
 "Description: " & wshRemote.Error.Description)
 End If

 If strDebug = "Enabled" Then
 MsgBox "Error Number: " & strErrorNo & vbCrLf & _
 "Line Number: " & wshRemote.Error.Line & vbCrLf & _
 "Description: " & wshRemote.Error.Description
 End If

 wshRemote.Terminate()

End Sub

The WriteToEventLog() Subroutine

The WriteToEventLog() subroutine, shown below, writes an informational message, passed to it as an argument, to the Windows application event log.

Sub WriteToEventLog(strMessage)

 WshShl.LogEvent 4, strMessage

End Sub

The TerminateScript() Subroutine

The TerminateScript() subroutine, shown below, uses the WScript object's Quit() method to terminate the controlling script's execution and to pass a return code back to the calling script. The return code sent back to the calling script is passed to this subroutine as an argument.

Sub TerminateScript(intRC)

 If strDebug = "Enabled" Then
 MsgBox "Script execution terminated.", , cTitleBarMsg
 End If

 WScript.Quit(intRC)

End Sub


The Fully Assembled Script

The fully assembled report distribution and remote archive management script is shown below. When executed on the Windows 2000 Professional workstation in the computer operation's command center, it will establish a temporary network connection to the corporate Web server and copy over the HTML and Word files representing the current day's summary report files. An updated copy of the Archive.html page is moved over to the Web server as well. In addition, on the first day of each month, this script will remotely execute and monitor a remote WSH VBScript on the Web server, which will maintain a three-month archive of the HTML and Word files.

'*************************************************************************
'Script Name: Script 32.1.vbs
'Author: Jerry Ford
'Created: 05/09/03
'Description: This script moves the Archive.html page and the current day's
'HTML and Word consolidated summary reports to the corporate web server
'*************************************************************************

'Initialization Section

Option Explicit

Dim strEventLog, strDebug, strHTMLFolder, strSharedRptFolder, strWebSvrName
Dim strConSolRptLoc, strSharedHTMLFolder, strWordRpt, strHTMLRpt, intReturnCode
Dim strResults

Const cTitleBarMsg = "Remote Archive Management Script"
Const strExecutionComplete = 2

Dim FsoObj, WshNtk, WshShl

Set FsoObj = CreateObject("Scripting.FileSystemObject")
Set WshNtk = WScript.CreateObject("WScript.Network")
Set WshShl = WScript.CreateObject("WScript.Shell")

intReturnCode = 0


'Main Processing Section

GetRegistrySettings()

If strEventLog = "Enabled" Then
 WriteToEventLog("Remote Archive Management Script now executing.")
End If

If strDebug = "Enabled" Then
 MsgBox "Remote Archive Management Script now executing.", , cTitleBarMsg
End If

MapNetworkDrive "W:", "\" & strWebSvrName & "" & strSharedRptFolder
MapNetworkDrive "X:", "\" & strWebSvrName & "" & strSharedHTMLFolder

GetFileNames()

CopyAndMoveFiles()

DisconnectNetworkDrive("W:")
DisconnectNetworkDrive("X:")

If strDebug = "Enabled" Then
 MsgBox "Remote Archive Management Script now completed.", , cTitleBarMsg
End If

If strEventLog = "Enabled" Then
 WriteToEventLog ("Remote Archive Management Script finished executing.")
End If

strResults = TimeToCleanArchive()

If strResults = "Yes" Then

 Set wshController = CreateObject("WshController")
 Set wshRemote = wshController.CreateScript("RemoteArchiveMgr.vbs", "\" & _
 strWebSvrName)
 WScript.ConnectObject wshRemote, "RemoteScript_"

 wshRemote.Execute

 Do Until wshRemote.Status = strExecutionComplete
 WScript.Sleep 2000
 Loop

End If

TerminateScript(intReturnCode)


'Procedure Section

Sub GetRegistrySettings()

 On Error Resume Next

 strEventLog = _
 WshShl.RegRead("HKLMSoftwareIntuitVBScriptsWebRptingEventLogging")
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog ("HTML Report Conversion Script - Using default " & _
 "for strEventLog. RC = 4")
 TerminateScript(12)
 End If
 End If

 strDebug = WshShl.RegRead("HKLMSoftwareIntuitVBScriptsWebRptingDebug")
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog ("HTML Report Conversion Script - Using default " & _
 "for strDebug. RC = 4")
 TerminateScript(12)
 End If
 End If

 strHTMLFolder = _
 "WshShl.RegRead("HKLMSoftwareIntuitVBScriptsWebRptingHTMLFolder")
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog ("HTML Report Conversion Script - Using default " & _
 "for strHTMLFolder. RC = 4")
 TerminateScript(12)
 End If
 End If

 strConSolRptLoc = _
 WshShl.RegRead("HKLMSoftwareIntuitVBScriptsWebRptingConSolRptLoc")
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog ("HTML Report Conversion Script - Using default " & _
 "for strConSolRptLoc. RC = 4")
 TerminateScript(12)
 End If
 End If

 strSharedRptFolder = _
 WshShl.RegRead("HKLMSoftwareIntuitVBScriptsWebRptingShare_Rpts")
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog ("HTML Report Conversion Script - Using default " & _
 "for strSharedRptFolder. RC = 4")
 TerminateScript(12)
 End If
 End If

 strSharedHTMLFolder = _
 WshShl.RegRead("HKLMSoftwareIntuitVBScriptsWebRptingShare_HTML")
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog ("HTML Report Conversion Script - Using default " & _
 "for strSharedHTMLFolder. RC = 4")
 TerminateScript(12)
 End If
 End If

 strWebSvrName = _
 WshShl.RegRead("HKLMSoftwareIntuitVBScriptsWebRptingWebServer")
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog ("HTML Report Conversion Script - Using default " & _
 "for strWebSvrName. RC = 4")
 TerminateScript(12)
 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 & _
 "strHTMLFolder" & vbTab & "=" & vbTab & strHTMLFolder & vbCrLf & _
 "strConSolRptLoc" & vbTab & "=" & vbTab & strConSolRptLoc & vbCrLf & _
 "strSharedRptFolder" & vbTab & "=" & vbTab & strSharedRptFolder & _
 "strSharedHTMLFolder" & vbTab & "=" & vbTab & strSharedHTMLFolder & _
 vbCrLf & "strWebSvrName" & vbTab & "=" & vbTab & strWebSvrName & _
 vbCrLf, ,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(4)

 End If

End Function

Sub GetFileNames()

 strHTMLRpt = Replace(Date(), "/", "-")

 strHTMLRpt = strHTMLRpt & "_ConsolSumRpt.html"

 strWordRpt = Replace(strHTMLRpt, "html", "doc")

 If strDebug = "Enabled" Then
 MsgBox "HTML Summary Report File Name = " & strHTMLRpt & vbCrLf & _
 "Word Summary Report File Name = " & strWordRpt, , cTitleBarMsg
 End If

End Sub

Sub CopyAndMoveFiles()

 Dim strFileName

 If (FsoObj.FileExists(strConSolRptLoc & "" & strHTMLRpt)) = "False" Then
 If strDebug = "Enabled" Then
 MsgBox "File " & strHTMLRpt & " not found. Stopping " & _
 "script execution.", , cTitleBarMsg
 End If
 If strEventLog = "Enabled" Then
 WriteToEventLog "Remote Archive Management Script failed. Unable " & _
 "find file: " & strHTMLRpt
 End If
 TerminateScript(4)
 End If
 If (FsoObj.FileExists(strConSolRptLoc & "" & strWordRpt)) = "False" Then
 If strDebug = "Enabled" Then
 MsgBox "File " & strWordRpt & " not found. Stopping " & _
 "script execution.", , cTitleBarMsg
 End If
 If strEventLog = "Enabled" Then
 WriteToEventLog "Remote Archive Management Script failed. Unable " & _
 "find file: " & strWordRpt
 End If
 TerminateScript(4)
 End If
 If (FsoObj.FileExists(strHTMLFolder & "" & "Archive.html")) = "False" Then
 If strDebug = "Enabled" Then
 MsgBox "File Archive.html not found. Stopping " & _
 "script execution.", , cTitleBarMsg
 End If
 If strEventLog = "Enabled" Then
 WriteToEventLog "Remote Archive Management Script failed. Unable " & _
 "find file: Archive.html"
 End If
 TerminateScript(4)
 End If

 Set strFileName = FsoObj.GetFile(strConSolRptLoc & "" & strHTMLRpt)
 strFileName.Move "W:"
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog "Remote Archive Management Script failed moving " & _
 strHTMLRpt
 End If
 If strDebug = "Enabled" Then
 MsgBox "Remote Archive Management Script failed moving " & _
 strHTMLRpt, , cTitleBarMsg
 End If
 TerminateScript(8)
 End If

 Set strFileName = FsoObj.GetFile(strConSolRptLoc & "" & strWordRpt)
 strFileName.Copy "W:"
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog "Remote Archive Management Script failed copying " & _
 strWordRpt
 End If
 If strDebug = "Enabled" Then
 MsgBox "Remote Archive Management Script failed moving " & _
 strWordRpt, , cTitleBarMsg
 End If
 TerminateScript(8)
 End If

 Set strFileName = FsoObj.GetFile("X:Archive.html")
 strFileName.Delete

 Set strFileName = FsoObj.GetFile(strHTMLFolder & "" & "Archive.html")
 strFileName.Move "X:"
 If Err <> 0 Then
 If strEventLog = "Enabled" Then
 WriteToEventLog "Remote Archive Management Script failed moving " & _
 "Archive.html"
 End If
 If strDebug = "Enabled" Then
 MsgBox "Remote Archive Management Script failed moving " & _
 "Archive.html", , cTitleBarMsg
 End If
 TerminateScript(8)
 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

Function TimeToCleanArchive()

 If Day(Date()) = 1 Then
 TimeToCleanArchive = "Yes"
 End If

End Function

Sub RemoteScript_Start()
 If strEventLog = "Enabled" Then
 WriteToEventLog("Remote Archive Management Script - started.")
 End If

 If strDebug = "Enabled" Then
 MsgBox "Remote Archive Management Script - started.", , cTitleBarMsg
 End If

End Sub

Sub RemoteScript_End()

 If strEventLog = "Enabled" Then
 WriteToEventLog("Remote Archive Management Script - stopped.")
 End If

 If strDebug = "Enabled" Then
 MsgBox "Remote Archive Management Script - stopped.", , cTitleBarMsg
 End If

End Sub

Sub RemoteScript_Error()

 strErrorNo = Hex(wshRemote.Error.Number)
 strErrorNo = CStr(strErrorNo)

 If strEventLog = "Enabled" Then
 WriteToEventLog ("Error Number: " & strErrorNo & vbCrLf & _
 "Line Number: " & wshRemote.Error.Line & vbCrLf & _
 "Description: " & wshRemote.Error.Description)
 End If

 If strDebug = "Enabled" Then
 MsgBox "Error Number: " & strErrorNo & vbCrLf & _
 "Line Number: " & wshRemote.Error.Line & vbCrLf & _
 "Description: " & wshRemote.Error.Description
 End If

 wshRemote.Terminate()

End Sub

Sub WriteToEventLog(strMessage)

 WshShl.LogEvent 4, strMessage

End Sub

Sub TerminateScript(intRC)

 If strDebug = "Enabled" Then
 MsgBox "Script execution terminated.", , cTitleBarMsg
 End If

 WScript.Quit(intRC)

End Sub


Creating the Archive Maintenance Script

The previous VBScript executes an archive maintenance script, which is shown below. This is a relatively small script with limited functionality. It begins by defining the two variables that represent the month's worth of archive files to be deleted and the location of the folder on the Web server where the archived files reside.

Alexander was informed by Michael Burns, the corporate Web master, that he was not permitted to make modifications to the Windows registry on the corporate Web server. Therefore, he decided to hard code the location of the archive folder within the script. The script's Main Processing Section consists of just three statements. The first statement calls the MonthToDelete() subroutine, which determines which files are eligible for deletion from the archive. The second statement calls the RemoveOldSummaryFiles() subroutine, which performs the actual deletion of the files. The third statement uses the WScript object's Quit() method to terminate the script's execution. The logic presented in the MonthToDelete() and RemoveOldSummaryFiles() subroutines has already been covered several times in previous examples within this book.

'*************************************************************************
'Script Name: Script 32.2.vbs
'Author: Jerry Ford
'Created: 05/09/03
'Description: This script deletes HTML and Word versions of the
'consolidated summary reports that are older than 3 months old
'*************************************************************************

'Initialization Section

Option Explicit

Dim strDeleteMonth, strHTMLFolder

strHTMLFolder = "d:IntuitOrderInventoryReportingRpts"


'Main Processing Section

MonthToDelete()

RemoveOldSummaryFiles()

WScript.Quit()


'Procedure Section

Sub MonthToDelete()

 Dim intGetSlashPosition, strCurrentMonth

 intGetSlashPosition = Instr(Date(), "/")

 strCurrentMonth = Mid(Date(), 1, intGetSlashPosition - 1)
 strDeleteMonth = strCurrentMonth - 4

 If strDeleteMonth = 0 Then
 strDeleteMonth = "12"
 End If
 If strDeleteMonth = -1 Then
 strDeleteMonth = "11"
 End If
 If strDeleteMonth = -2 Then
 strDeleteMonth = "10"
 End If
 If strDeleteMonth = -3 Then
 strDeleteMonth = "9"
 End If

End Sub

Sub RemoveOldSummaryFiles()

 Dim FsoObject, strSummaryRptPath
 Set FsoObject = WScript.CreateObject("Scripting.FileSystemObject")

 FsoObject.DeleteFile strHTMLFolder & strDeleteMonth & "*"

End Sub


Summary

In this chapter, you learned how to work with Remote WSH. This included a detailed examination of the WshController, WshRemote, and WshRemoteError objects and their methods and properties. You then learned how to apply Remote WSH to perform the remote file administration of the Word and HTML files on the Intuit corporate Web server. With the information presented in this chapter, you now have the background you need to develop scripts that can remotely administer any number of remote computers from a single Windows computer.






Microsoft VBScript Professional Projects
Microsoft VBScript Professional Projects
ISBN: 1592000568
EAN: 2147483647
Year: 2005
Pages: 366
Simiral book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net