intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Using Windows Script Host to Modify the Registry phần 1

Chia sẻ: Nghia Tuan | Ngày: | Loại File: PDF | Số trang:8

135
lượt xem
18
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Using Windows Script Host to Modify the Registry In Chapter 10 we already touched on the problem of using Windows Script Host (WSH) and even provided a small code excerpt

Chủ đề:
Lưu

Nội dung Text: Using Windows Script Host to Modify the Registry phần 1

  1. Using Windows Script Host to Modify the Registry In Chapter 10 we already touched on the problem of using Windows Script Host (WSH) and even provided a small code excerpt, which illustrated its usage. Introduced with Windows 2000, WSH is capable of creating simple, but flexible and powerful scripts to automate network administration. Because WSH is language-independent, you're free to select any scripting language you prefer: Visual Basic Scripting Edition (VBScript), JScript, or Perl. WSH also supports COM, allowing you to enjoy the advantages of new technologies such as Windows Management Instrumentation (WMI). Now, the time has come to consider this topic in more detail. Of course, it is impossible to provide a detailed description of WSH, WMI, or scripting languages, such as VBScript or JScript, within a single chapter (after all, each of these topics deserves a separate book, and quite a comprehensive one, since I have encountered volumes of JScript and Perl that comprised more than 1,500 pages). Therefore, if you want a detailed language reference, simply buy one of those books at your local bookstore. However, we will consider registry-related topics, and, in particular, the RegRead, Regwrite, and RegDelete methods provided by WSH. We will also consider their practical usage and provide several simple, but useful scripts. Basic Information on Microsoft Windows Script Host WSH is a language-independent scripting host for Windows Script-compatible scripting engines. It brings simple, powerful, and flexible scripting to the Windows 32-bit platform, allowing you to run scripts from both the Windows desktop and the command prompt. Windows Script Host is ideal for non-interactive scripting needs such as logon scripting, administrative scripting, and machine automation. The Benefits of Windows Script Host WSH offers the following benefits: Two ways to run scripts, WScript.exe and CScript.exe. WScript.exe provides a Windows-based properties page for setting script properties; CScript.exe provides command-line switches for setting script properties. Support for multiple files. You can call multiple scripting engines and perform multiple jobs from a single Windows Script (WSF) file. Low memory requirements. Mapping of script extensions to programmatic identifiers (ProglDs). When you start a script from the Windows desktop or the command prompt, the script host
  2. reads and passes the specified script file contents to the registered script engine. Instead of using the HTML SCRIPT tag to identify the script, the host uses file extensions; for example, VBS for Microsoft Visual Basic® Scripting Edition (VBScript) files, and JS for Microsoft JScript® files. Using extensions means you no longer need to be familiar with the ProgID for a given script engine. Windows Script Host handles this for you by maintaining a mapping of script extensions to ProglDs, launching the appropriate engine for a given script. Windows XP includes the latest version of Windows Script Host - version 5.6.0 (Fig. 15.1). Versions of Windows Script Host implemented by Microsoft operating systems are listed in Table 15.1. Figure 15.1: Windows XP and Windows Server 2003 include the latest version of Windows Script Host - version 5.6.0 Table 15.1: WSH Versions implemented by Microsoft Operating Systems Host Application 1.0 2.0 5.1 5.6 Microsoft Windows 98 x
  3. Table 15.1: WSH Versions implemented by Microsoft Operating Systems Host Application 1.0 2.0 5.1 5.6 Microsoft Windows ME x Microsoft Windows NT 4 Option Pack x Microsoft Windows 2000 x Microsoft Windows XP x Microsoft Windows Server 2003 x In comparison to previous versions, this one provides the following enhancements in functionality. Argument-handling has been improved - handling and documenting command line arguments is simpler. The process of integrating your scripts with other command line scripts has been simplified, and it is easier to create scripts that can supply the user with help information. Refer to the following table for information on the WSH language features that connect you to this new functionality. You can run scripts remotely - you can load scripts onto several remote computer systems, and start them all running simultaneously. While a remote script is running, you can check on its progress, and after it has finished, you can ensure that it has run correctly, or find out what caused it to terminate prematurely. There is a new dispatch object used to create remote WSH objects - the controller object. In addition, there is a new object that represents an instance of a running script - the Remote WSH object. When you start new processes, you can treat them as objects - you determine the status of spawned processes, and access their standard I/O streams. You can access the current working directory - you can determine/modify the active process' current working directory. Security issues unique to scripts have been addressed - Windows Script Host, a powerful, flexible tool for automating Windows, can at the same time be dangerous if used improperly or with malicious intentions. Windows Script Host 5.6, included with Windows XP and Windows Server 2003, implements a new security model, which enables users to verify the authenticity of a script before running it. Script developers can sign their scripts to prevent unauthorized modifications. Administrators can enforce strict policies that determine which users have privileges to run scripts locally or remotely. Note Windows provides a standard mechanism for signing code via signcode.exe. Unfortunately, signcode.exe doesn't ship with Windows, but rather with the Windows SDK. The most useful and interesting article on this important topic can be downloaded from http://msdn.microsoft.com/library/defaultasp?url=/library/en- us/dnclinic/html/scripting10082001.asp
  4. Windows Script Host Object Model As everything in modern Microsoft operating systems, WSH is object-oriented. The Windows Script Host object model consists of 14 objects. The root object is the WScript object. The Windows Script Host object model provides a logical, systematic way to perform many administrative tasks. The set of COM interfaces it provides can be placed into two main categories: Script Execution and Troubleshooting. This set of interfaces allows scripts to perform basic manipulation of the Windows Script Host, output messages to the screen, and perform basic COM functions such as Createobject and GetObject. Helper Functions. Helper functions are properties and methods for performing actions such as mapping network drives, connecting to printers, retrieving and modifying environment variables, and manipulating registry keys. Administrators can also use the Windows Script Host helper functions to create simple logon scripts. Note For purposes of accessing the registry, the most important object is Wshshell, which will be discussed in the next section. WshShell Object Provides access to the native Windows shell. The WshShell object is a child object of the WScript object - you must use the WScript method CreateScript to create a WshShell object (i.e., WScript.CreateObject('WScript.Shell')). You create a WshShell object whenever you want to run a program locally, manipulate the contents of the registry, create a shortcut, or access a system folder. The WshShell object provides the Environment collection. This collection allows you to handle environmental variables (such as WINDIR, PATH, or PROMPT). RegRead Method The RegRead method returns the value of a key or value name from the registry. This method uses the following syntax: Object.RegRead(strName) where: Object - WshShell object strName - string value indicating the key or value-name whose value you want
  5. The RegRead method can return the values of the following data types: REG_SZ, REG_DWORD, REG_BINARY, REG_EXPAND_SZ, and REG_MULTI_SZ. You can specify a key name by ending strName with a final backslash. Do not include a final backslash to specify a value name. A value entry has three parts: its name, its data type, and its value. When you specify a key name (as opposed to a value name), RegRead returns the default value. To read a key's default value, specify the name of the key itself. Fully qualified key names and value names begin with a root key. You must use abbreviated versions of root key names with the RegReadmethod. The five possible root keys are listed in Table 15.2. Table 15.2: Abbreviations for the Registry Root Key Names Root Key Name Abbreviation HKEY_CURRENT_USER HKCU HKEY_LOCAL_MACHINE HKLM HKEY_CLASSES_ROOT HKCR HKEY_USERS HKEY_USERS HKEY_CURRENT_CONFIG HKEY_CURRENT_CONFIG RegWrite Method The RegWrite Method creates a new key, adds another value to an existing key (and assigns it a value), or changes the value of an existing value name. This method uses the following syntax: Object.RegWrite(strName, anyValue [,strType]) where: Object - WshShell object strName - string value indicating the key name, value name, or value you want to create, add, or change anyValue - the name of the new key you want to create, the name of the value you want to add to an existing key, or the new value you want to assign to an existing value name strType - optional: string value indicating the value's data type
  6. Specify a key name by ending strName with a final backslash. Do not include a final backslash to specify a value name. The RegWrite method automatically converts the parameter anyValue to either a string or an integer. The value of strType determines its data type (either a string or an integer). The options for strType are listed in Table 15.3. Table 15.3: Acceptable Values of the strType Argument for the RegWrite Method Converted to strType string REG_SZ string REG_EXPAND_SZ integer REG_DWORD string REG_BINARY Note The REG_MULTI_SZ type is not supported for the RegWrite method. Fully qualified key names and value names are prefixed with a root key. You must use abbreviated versions of root key names (if one exists) with the RegWrite method. Abbreviated names of the registry root keys used by the RegWrite method are the same as those for the RegRead method. RegDelete Method The RegDelete method is used to delete a registry key or one of its values from the registry. This method uses the following syntax: Object.RegDelete (strName) where: Object - WshShell object strName - string value indicating the name of the registry key or key value you want to delete Specify a key-name by ending strName with a final backslash; leave it off to specify a value name. Fully qualified key names and value names are prefixed with a root key. You must use abbreviated versions of root key names (if one exists) with the RegDelete method. There are five possible root keys you can use; they are the same as those for the RegRead and RegWrite methods. JScript Example
  7. A simple example written in JavaScript (JScript in Microsoft's implementation), illustrating the usage of these methods is provided in Listing 15.1. This code creates a registry key HKEY_CURRENT_USER\Software\MyCoolSoftware, sets its Default value (REG_BINARY data type) to 1, then creates another REG_SZ value entry under this key and assigns it the "This is a test!" string value. Listing 15.1: JScript Example Illustrating Registry Access // The simplest example illustrating registry access using JScript // Use this module at your own risk // Setting variables var vbOKCancel = 1; var vblnformation = 64; var vbCancel = 2; var result; // Creating wshShell object var WshShell = WScript.CreateObject ("WScript.Shell"); { // prompting the user result = WshShell.Popup ("Do you want to create a new registry setting?", 0, "Registry Access using JScript", vbOKCancel + vblnformation); if (result != vbCancel) { WshShell.RegWrite ("HKCU\\Software\\MyCoolsoftware\\", 1, "REG_BINARY"); WshShell.RegWrite ("HKCU\\Software\\MyCoolSoftware\\MySuperProgram", "This is a test!", "REG_SZ"); var bKey = WshShell.RegRead ("HKCU\\Software\\MyCoolSoftware\\"); WScript.Echo (WshShell.RegRead ("HKCU\\Software\\MyCoolSoftware\\ MySuperProgram")); } //prompting the user result = WshShell.Popup("Do you want to delete newly created settings?", 0, "Registry Access using JScript", vbOKCancel + vblnformation); if (result != vbCancel) { WshShell.RegDelete ("HKCU\\Software\\MyCoolSoftware\\MySuperProgram"); WshShell.RegDelete ("HKCU\\Software\\MyCoolSoftware\\"); } } To test this script, enter the code provided in this listing using any text editor (for example, Notepad.exe), and save the file with the JS filename extension. If you double- click this file, WSH server will start and execute the script. Notice that this script prompts
  8. the user to confirm adding new registry entries (Fig. 15.2), displays the contents of the newly created registry entry (Fig. 15.3) and then asks the user if the newly created registry key and value entry contained within it should be deleted (Fig. 15.4).
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2