1. Trang chủ
  2. » Công Nghệ Thông Tin

Microsoft WSH and VBScript Programming for the Absolute Beginner Part 46 ppsx

10 283 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 201,67 KB

Nội dung

430 Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition Summary In this final chapter you learned how to combine two or more different scripts into a single script using Windows Script Files. Windows Script Files are created using XML and a com- bination of different scripting languages. In addition, you got the opportunity to demonstrate your ability to work with Windows Script Files by developing the VBScript Game Console. At this point, you should have a solid understanding of both VBScript and the Windows Script Host and should feel confident, not just of your game development capabilities, but also in your ability to apply the knowledge and skills that you’ve learned here in real-world situations. C HALLENGES 1. Modify the information presented in the AboutFunction() and HelpFunction() procedures to make them more useful. 2. Modify the Windows Script File so the Windows Game Console script does not have to reside in the same folder as the games it supports, and then modify the script to retrieve the location of VBScript game folder from the Windows registry. 3. Right now, the VBScript Game Console script only works with VBScripts. Modify it so it will support any other script types you plan to work with (such as Windows Script Files). Part Appendix A: WSH Administrative Scripting Appendix B: Built-In VBScript Functions Appendix C: What’s on the Companion Web Site? Appendix D: What Next? Appendices IV This page intentionally left blank WSH Administrative Scripting A CHAPTERAPPENDIX I n this book, you learned a great deal about both VBScript and the WSH by developing computer games. In the real world, of course, VBScript and the WSH are used to automate tasks. These tasks are typically mundane, repetitive, and time-consuming, or extremely complex and therefore subject to human error. Automating such tasks using VBScript and the WSH makes perfect sense. The purpose of this appendix is to provide you with a collection of sample scripts that demonstrate some real-world tasks that can be scripted. None of the VBScripts that you will see in this appendix should be considered fin- ished products. For example, you won’t see any complex programming logic or a lot of error checking. These scripts were developed on a computer running Win- dows XP; you should review and test the scripts before running them on other operating systems. My intention for providing these sample scripts is to give you a feel for some of the real-world tasks that you can automate using VBScript and the WSH. I wanted to provide you with a collection of starter scripts from which you can begin to create and develop your own collection of scripts. I won’t spend a lot of time going over the development of these scripts, nor will I attempt to explain every operation they perform. By now, you should be able to look at each of these scripts and determine what it is doing. To help you out a lit- tle, I made sure to include plenty of comments. 434 Desktop Administration The administration of the Windows desktop on a single computer isn’t terribly time- consuming. However, for those responsible for the maintenance and care of a large number of computers, scripting is a godsend. For example, a lot of small companies purchase their computers directly from the manufacturer. These computers arrive with the operating system already installed. However, desktop settings such as the color of the Windows desktop background or screen saver settings will vary depending on just how the computer manu- facturer chose to set them up. Companies often try to keep the configuration of their computer settings standardized. This makes maintaining their computers easier and reduces a lot of user confusion. One way of configuring computers in this scenario is to develop VBScripts that automate the configu- ration of desktop settings according to company policy. Then, all you need to do to prepare a new computer for deployment is to copy over the scripts and have the user run them the first time he or she logs on to the computer. Configuring the Desktop Background The following VBScript demonstrates how to use the WshShell object’s RegWrite() method to configure values that are stored in the Windows Registry and affect the Windows desktop background: ‘************************************************************************* ‘Script Name: Background.vbs ‘Author: Jerry Ford ‘Created: 12/07/02 ‘Description: This script changes the user’s background selection to none ‘and sets the default background color to white. ‘************************************************************************* ‘Initialization Section Option Explicit On Error Resume Next Dim objWshShl, intChangeSettings Set objWshShl = WScript.CreateObject(“WScript.Shell”) Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition ‘Main Processing Section ‘Verify that the user intends to change his or her screen saver settings intChangeSettings = PromptForConfirmation() If intChangeSettings = 6 Then ModifySettings() End If WScript.Quit() ‘Procedure Section ‘This function determines if the user wishes to proceed Function PromptForConfirmation() PromptForConfirmation = MsgBox(“Set standard desktop background?”, 36) End Function ‘This subroutine alters screen saver settings Sub ModifySettings() ‘Turn off the wallpaper setting objWshShl.RegWrite “HKCU\Control Panel\Desktop\Wallpaper”, “” ‘Setting the background color to white objWshShl.RegWrite “HKCU\Control Panel\Colors\Background”, “255 255 255” End Sub The script begins by prompting for confirmation and then proceeds to modify the following Registry values: objWshShl.RegWrite “HKCU\Control Panel\Desktop\Wallpaper”, “” objWshShl.RegWrite “HKCU\Control Panel\Colors\Background”, “255 255 255” The first statement sets the Windows Wallpaper setting to “”. This is equivalent to right-click- ing on the Windows desktop, selecting Properties, and then setting the Background setting on the Desktop property sheet of the Windows Display Properties dialog to None. 435 Appendix A • WSH Administrative Scripting 436 The second statement sets the value of the Background setting to 255 255 255. This is the equivalent of selecting white as the color setting on the Desktop property sheet. To test this script, run it and then log off and on again. Configuring the Screen Saver The following VBScript demonstrates how to change the configuration of the Windows screen saver. The overall construction of this script is very similar to the previous example, the only difference being which Registry keys are edited. ‘************************************************************************* ‘Script Name: ScreenSaver.vbs ‘Author: Jerry Ford ‘Created: 12/07/02 ‘Description: This script changes the user’s screen saver to a default ‘collection of settings. ‘************************************************************************* ‘Initialization Section Option Explicit On Error Resume Next Dim objWshShl, intChangeSettings Set objWshShl = WScript.CreateObject(“WScript.Shell”) ‘Main Processing Section ‘Verify that the user intends to change his or her screen saver settings intChangeSettings = PromptForConfirmation() If intChangeSettings = 6 Then ModifySettings() End If Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition WScript.Quit() ‘Procedure Section ‘This function determines if the user wishes to proceed Function PromptForConfirmation() PromptForConfirmation = _ MsgBox(“Set standard screen saver settings?”, 36) End Function ‘This subroutine alters screen saver settings Sub ModifySettings() ‘Enables the Windows screen saver objWshShl.RegWrite “HKCU\Control Panel\Desktop\ScreenSaveActive”, 1 ‘Turns on password protection objWshShl.RegWrite “HKCU\Control Panel\Desktop\ScreenSaverIsSecure”, 1 ‘Establishes a 10-minute inactivity period objWshShl.RegWrite “HKCU\Control Panel\Desktop\ScreenSaveTimeOut”, 600 ‘Enable the Starfield screen saver objWshShl.RegWrite “HKCU\Control Panel\Desktop\SCRNSAVE.EXE”, _ “C:\Windows\System32\ssstars.scr” End Sub As you can see, this script modifies four Registry values. The modification of the first value enables the Windows screen saver. The modification of the second value enables screen saver password protection, which means that if the screen saver kicks in, the user has to retype his password to get back into Windows. The third modification sets up the screen saver to begin running after a 10-minute period of user inactivity. Finally, the last modifi- cation selects the screen saver that is to be run. To test this script, run it and then log off and on again. 437 Appendix A • WSH Administrative Scripting 438 Network Administration Network administration means many things to many people. For one thing, it may mean establishing connections to network drives so that a script can move, copy, create, and delete files and folders residing on network computers. Network management also means establishing or removing connections to network printers. In the next several sections, I’ll provide you with scripts that demonstrate how to connect to, and disconnect from, network drives and printers. Mapping Network Drives When you create a connection to a network drive (known as mapping), you make the network drive look as if it were local to your computer by assigning it a local drive letter—that is, as long as you have the appropriate set of security permissions on the network drive. Con- necting to and disconnecting from a network drive is achieved using methods belonging to the WshNetwork object. Mapping to Network Drives To create a drive mapping, you must use the WshNetwork object’s MapNetworkDrive() method: WshNetwork.MapNetworkDrive letter, name, [persistent], [username], [password] Letter is an available logical disk drive letter on your computer. Name is the UNC (universal naming convention) name and network path of the network drive. Persistent is optional; it determines whether or not the mapping is permanent. A value of True creates a permanent mapping. The default value of this setting is False, which causes the connection to last only for the current working session. Username and password are optional and are used to supply the username and password required to access the drive. When you run scripts from the Windows desktop or command line, they exe- cute using your security credentials. However, if you schedule the execution of your VBScript, then your scripts will not have the authority that you have and will be unable to establish a network drive connection. One way to get around this is to embed a username and password inside your script. However, doing so is really bad for security. Another option is to set up your script to prompt for a valid username and password at execution time and authorize someone who might be around to supply these credentials. TRAP Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition The following VBScript demonstrates how to establish a temporary network drive mapping: ‘************************************************************************* ‘Script Name: DriveMapper.vbs ‘Author: Jerry Ford ‘Created: 12/07/02 ‘Description: This script demonstrates how to add logic to VBScripts in ‘order to support network drive mapping. ‘************************************************************************* ‘Initialization Section Option Explicit On Error Resume Next Dim objWshNet ‘Instantiate the objWshNetwork object Set objWshNet = WScript.CreateObject(“WScript.Network”) ‘Main Processing Section ‘Call the procedure that maps drive connections passing it an available ‘drive letter and the UNC pathname of the drive MapNetworkDrive “z:”, “\\ICS_Server\D” WScript.Quit() ‘Terminate script execution ‘Procedure Section ‘This subroutine creates network drive mappings Sub MapNetworkDrive(DriveLetter, NetworkPath) 439 Appendix A • WSH Administrative Scripting . about both VBScript and the WSH by developing computer games. In the real world, of course, VBScript and the WSH are used to automate tasks. These tasks are typically mundane, repetitive, and time-consuming,. Explicit On Error Resume Next Dim objWshShl, intChangeSettings Set objWshShl = WScript.CreateObject(“WScript.Shell”) Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition ‘Main. settings intChangeSettings = PromptForConfirmation() If intChangeSettings = 6 Then ModifySettings() End If Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition WScript.Quit() ‘Procedure

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

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN