Microsoft WSH and VBScript Programming for the Absolute Beginner Part 48 pptx

10 322 0
Microsoft WSH and VBScript Programming for the Absolute Beginner Part 48 pptx

Đang tải... (xem toàn văn)

Thông tin tài liệu

450 On Error Resume Next Dim objWshShl ‘Instantiate the WshShell object Set objWshShl = WScript.CreateObject(“WScript.Shell”) ‘Main Processing Section ‘Call the procedure that schedules the execution of VBSCripts ScheduleTheScripts() WScript.Quit() ‘Terminate script execution ‘Procedure Section Sub ScheduleTheScripts() ‘Use the WshShell object’s Run() method to run the AT command objWshShl.Run “at 20:00 /every:M,T,W,Th,F cmd /c GenerateRpts.vbs”, 0, “True” objWshShl.Run “at 21:00 /every:M,T,W,Th,F cmd /c CopyFiles.vbs”, 0, “True” objWshShl.Run “at 22:00 /every:M,T,W,Th,F cmd /c ClearOutFolders.vbs”, 0, “True” End Sub The Windows Scheduler If you prefer to point and click your way through the setup of an automation schedule for your VBScripts, then you can use the Windows Scheduled Task Wizard. To start this wizard on a computer running Windows XP, click on Start, Control Panel, Performance and Maintenance, and then Scheduled Tasks. This opens the Scheduled Tasks folder, as shown in Figure A.1. To set up a new scheduled task for one of your VBScripts, double-click on the Add Scheduled Task icon. This starts the Scheduled Task Wizard. Click on Next and follow the instructions presented by the wizard. Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition Creating a Master Scheduling Script Another option for managing the execution of your VBScripts is to create a single script that you schedule using either the AT command or the Windows Scheduler. This master scheduling script can then manage the execution of your other scripts as demonstrated in the following example: 451 Appendix A • WSH Administrative Scripting Figure A.1 Using the Scheduled Tasks folder to view the three scheduled tasks set up by the previous VBScript. In the Real World Scripts that you execute manually run using the security permissions assigned to you. However, tasks run using the Windows Task Scheduler services will not, by default, have this same level of access. One way to get around this problem is to associate a specific username and password with each scheduled task. You can do this from the Scheduled Tasks folder by right-clicking on a scheduled task and specifying a username in the Run as field, and then clicking on Password and typing in the password associated with the account. 452 ‘************************************************************************* ‘Script Name: MasterScheduler.vbs ‘Author: Jerry Ford ‘Created: 11/13/04 ‘Description: This script manages the controlled execution of other ‘VBScripts. It runs these VBScripts one at a time in sequential order. ‘************************************************************************* ‘Initialization Section Option Explicit On Error Resume Next Dim objWshShl Set objWshShl = WScript.CreateObject(“WSCript.shell”) ‘Main Processing Section RunScript(“TestScript1.vbs”) RunScript(“TestScript2.vbs”) RunScript(“TestScript3.vbs”) WScript.Quit() ‘Terminate script execution ‘Procedure Section Function RunScript(ScriptName) objWshShl.Run ScriptName, 1, True End Function Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition The WshShell object’s Run() method makes this script work. You can use this method to start a VBScript and then wait for it to finish executing before starting the execution of another script. This example is fairly basic; you can easily adapt it to selectively executing your VBScripts. For example, the following statements could be added to the script to limit the execution of a VBScript to the 15th day of the month: If Day(Date()) = 15 Then RunScript(“TestScript4.vbs”) End If Disk Management VBScripts provide an excellent tool for automating the execution of various Windows sys- tem administration utilities. Examples of two such utilities are the Windows Disk Cleanup and Defrag utilities. Like many Windows utilities, these utilities provide a command-line interface, meaning that you can control their execution via your scripts using the WshShell object’s Run() method. The Disk Cleanup and Disk Defrag utilities are perfect candidates for VBScript automation because they consume so many system resources when they exe- cute that it’s difficult to use the computer while they are running. By scripting their execution and then scheduling your scripts to run when you are not using your computer, you can keep your computer’s disk drive in good shape without major inconvenience. Automating Disk Cleanup The Windows disk cleanup utility recovers lost disk space by deleting unnecessary files stored on the computer’s disk drive. When executed, the disk cleanup utility deletes the fol- lowing files: • Files found in the Recycle Bin • Temporary files • Downloaded program files • Temporary Internet files • Catalog files for the Content Indexer • WebClient/Publisher temporary files TRICK 453 Appendix A • WSH Administrative Scripting 454 Before you can automate the execution of the disk cleanup utility, you must perform a one- time configuration process as outlined here: 1. Click on Start and then Run. The Run dialog opens. 2. Type cleanmgr /sageset:1 and then click on OK. 3. The Disk Cleanup dialog opens. Select the types of files that you want the disk cleanup process to remove and then click on OK. After you’ve completed the configuration process, you can create your disk cleanup execu- tion script using the following example as a template: ‘************************************************************************* ‘Script Name: VBSCleanup.vbs ‘Author: Jerry Ford ‘Created: 11/13/04 ‘Description: This script automates the execution of the Windows Cleanup ‘ utility. ‘************************************************************************* ‘Initialization Section Option Explicit Dim objWshShl Set objWshShl = WScript.CreateObject(“WScript.Shell”) ‘Main Processing Section ExecuteCleanupUtility() RecordMsgToAppEventLog() WScript.Quit() ‘Terminate the script’s execution ‘Procedure Section Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition Function ExecuteCleanupUtility() ‘Run the Windows Disk Cleanup utility objWshShl.Run “C:\WINDOWS\SYSTEM32\cleanmgr /sagerun:1” End Function Function RecordMsgToAppEventLog() ‘Record message in Application event log objWshShl.LogEvent 4, “VBSCleanup.vbs - Disk cleanup has been started.” End Function When you create your script make sure that you specify the /sagerun:1 parameter exactly as shown here: WshShl.Run “C:\WINDOWS\SYSTEM32\cleanmgr /sagerun:1” Automating the Disk Defrag Process The disk defrag process speeds up the time it takes for your computer to store and retrieve files on the hard drive by reorganizing files that become scattered all over the drive as space becomes scarce. To run the Windows defrag process from within a VBScript, you need to know the syntax of the defrag.exe command, which is listed here: defrag <volume:> [/a] [/f] [/v] Volume specifies the disk drive to be worked on. The /a parameter displays an analysis of the drive’s fragmentation status. By default, the defrag command will not run unless at least 15 percent of free space is available on the specified disk drive. When specified, the /f para- meter forces defrage.exe to execute when less than 15 percent of free space is available. When specified, the /v parameter causes verbose output to be displayed. If disk space is tight, schedule the execution of the disk cleanup process before running your disk defrag script to ensure that as much free space as possible is available. TRICK 455 Appendix A • WSH Administrative Scripting 456 The following script demonstrates how you can automate the execution of the defrag process: ‘************************************************************************* ‘Script Name: DiskDefrag.vbs ‘Author: Jerry Ford ‘Created: 11/13/04 ‘Description: This script executes the Defrag.exe (Windows Defrag ‘ utility). ‘************************************************************************* ‘Initialization Section Option Explicit Dim objWshShl Set objWshShl = WScript.CreateObject(“WScript.Shell”) ‘Main Processing Section RunDefragUtility() RecordMsgToAppEventLog() WScript.Quit() ‘Terminate the script’s execution ‘Procedure Section Function RunDefragUtility() ‘Run the defrag.exe command-line utility objWshShl.Run “c:\Windows\System32\defrag C: /f” End Function Function RecordMsgToAppEventLog() ‘Record message in Application event log objWshShl.LogEvent 4, “VBSCleanup.vbs - Disk cleanup has been started.” End Function Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition As you can see, the script executes the defrag command using the WshShell object’s Run() method, specifying the /f parameter. Integrating VBScript with Other Applications In addition to creating VBScripts that can interact with and control Windows resources, you can also create VBScripts that automate the execution of popular Windows applications such as Microsoft Word, Microsoft Excel, and WinZip. In this section you’ll see examples of how to use VBScript and the WSH to create a Word document, an Excel spreadsheet, and a Zip file. Automating Microsoft Word Reports Generation To use VBScript to automate Word tasks, you need to know a little something about the Word object model. The Application object resides at the top of the Word object model. The Application object is automatically instantiated when Word is started. Using properties and methods associated with the Application object, you can access lower-level objects and col- lections in the Word model, and using the properties and methods associated with the lower-level objects, you can automate any number of Word tasks. The following script provides you with a working example of how to use VBScript and the WSH to automate the creation of a Word document. Comments embedded within the script provide additional information about the Word object model. The Word object model is far too large and detailed to be covered in this book. You can learn more about it by visiting http://msdn.Microsoft.com/office. ‘************************************************************************* ‘Script Name: WordObjectModelExample.vbs ‘Author: Jerry Ford ‘Created: 11/13/04 ‘Description: This script demonstrates how to use integrate VBScript and ‘ the Microsoft Word Object model. ‘************************************************************************* ‘Initialization Section Option Explicit TRICK 457 Appendix A • WSH Administrative Scripting 458 On Error Resume Next Dim objWord ‘Used to establish a reference to Word Application object Set objWord = WScript.CreateObject(“Word.Application”) ‘Instantiate Word ‘Main Processing Section CreateNewWordDoc() WriteWordReport() SaveWordDoc() CloseDocAndEndWord() TerminateScript() ‘Procedure Section Function CreateNewWordDoc() ‘Documents is a collection. Add() is a method belonging to the Documents ‘collection that opens a new empty Word document objWord.Documents.Add() End Function Function WriteWordReport() ‘Specify Font object’s Name, Size, Underline, & Bold properties objWord.Selection.Font.Name = “Arial” objWord.Selection.Font.Size = 16 objWord.Selection.Font.Underline = True objWord.Selection.Font.Bold = True Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition ‘Use the Selection object’s Typetext() method to write text output objWord.Selection.Typetext(“Sample VBScript Word Report”) ‘Use the Selection object’s TypeParagraph() method to insert linefeeds objWord.Selection.TypeParagraph objWord.Selection.TypeParagraph objWord.Selection.TypeParagraph ‘Use the Font object’s Underline & Bold properties objWord.Selection.Font.Underline = False objWord.Selection.Font.Bold = False ‘Use the Font object’s Size & Bold properties objWord.Selection.Font.Size = 12 objWord.Selection.Font.Bold = False ‘Use the Selection object’s Typetext() method to write text output objWord.Selection.Typetext(“Prepared on “ & Date()) ‘Use the Selection object’s TypeParagraph() method to insert linefeeds objWord.Selection.TypeParagraph objWord.Selection.TypeParagraph ‘Use the Selection object’s Typetext() method to write text output objWord.Selection.Typetext(“CopyRight - Jerry Lee Ford, Jr.”) End Function Function SaveWordDoc() ‘The Applications object’s ActiveDocument property established a ‘reference to the current Word document. ‘The Document object’s SaveAs() method provides the ability to save ‘the Word file 459 Appendix A • WSH Administrative Scripting . Function Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition As you can see, the script executes the defrag command using the WshShell object’s Run() method, specifying the. Scheduled Task icon. This starts the Scheduled Task Wizard. Click on Next and follow the instructions presented by the wizard. Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition Creating. Section Function RunScript(ScriptName) objWshShl.Run ScriptName, 1, True End Function Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition The WshShell object’s Run() method

Ngày đăng: 03/07/2014, 18:20

Tài liệu cùng người dùng

Tài liệu liên quan