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 2

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

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

Figure 15.2: The dialog prompting the user to confirm creating of a new registry setting Figure 15.3: Displaying the contents of the newly created registry entry

Chủ đề:
Lưu

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

  1. Figure 15.2: The dialog prompting the user to confirm creating of a new registry setting Figure 15.3: Displaying the contents of the newly created registry entry Figure 15.4: The dialog prompting the user to confirm deletion of the newly created registry setting(s) These dialog boxes allow the user to check modifications introduced to the registry at each step, using, for example, Registry Editor (Fig. 15.5). Figure 15.5: You can use Registry Editor to check modifications introduced to the registry at each step of the script VBScript Examples
  2. If you prefer VBScript, you can also use the above-described methods for accessing the registry (notice the difference in the syntax of JScript and VBScript). Enabling and Disabling Changes to the Start Menu A small example is provided below, developed using VBScript, which, in contrast to the previous one, does something useful - it enables or disables changes to the Start Menu. In the previous chapter, we discussed the values that control the Start menu. One such value is the NoChangeStartMenu under HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\E xplorer. When this value is set to 1, one cannot make changes, and when this value is set to 0, changes are allowed. Our small VBScript example first displays the dialog prompting the user to choose whether he or she needs to lock the Start menu (Fig. 15.6). To manage the Start menu via the system registry, the script creates the NoChangeStartMenu value, and sets it to 1 if the user chooses to lock the Start menu. If the user clicks No, the NoChangeStartMenu value will be set to 0. Next, the script reads the NoChangeStartMenu value from the registry, displays the current Start menu status, and prompts the user to change it if desired (Fig. 15.7). Figure 15.6: Prompt for the user to lock Start menu Figure 15.7: Prompt for the user to unlock Start menu The source code for this example is provided in Listing 15.2. Listing 15.2: Source Code for the VBScript Example that Enables or Disables Changes to the Start Menu Option Explicit Dim WSHShell, RegKey, NoChangeStartMenu, Result Set WSHShell = CreateObject ("WScript.Shell") RegKey =
  3. "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\" Result = MsgBox("Would you like to lock your start Menu?", 36) If Result = 6 Then 'clicked yes WSHShell.RegWrite regkey & "NoChangeStartMenu", 1 Else WSHShell.RegWrite regkey & "NoChangeStartMenu", 0 End If NoChangeStartMenu = WSHShell.RegRead (regkey & "NoChangeStartMenu") If NoChangeStartMenu = 1 Then 'Start Menu is locked Result = MsgBox("Your Start Menu is currently locked." & _ vbNewLine & "Would you like to unlock?", 36) If Result = 6 Then 'clicked yes WSHShell.RegWrite regkey & "NoChangeStartMenu", 0 End If Else 'Start menu can be changed Result = MsgBox("You can change Start menu." & _ vbNewLine & "Would you like to prohibit changes", 36) If Result = 6 Then 'clicked yes WSHShell.RegWrite regkey & "NoChangeStartMenu", 1 End If End If ' End code Managing System Restore on Windows XP Clients The example presented in this section illustrates how you can use Windows Management Instrumentation to automate your work with the System Restore feature on client workstations running Windows XP. Before we proceed any further, let us provide a brief description of WMI scripting capabilities utilization. WMI scripting is a library of automation interfaces. COM- compliant scripting languages use these automation interfaces to access WMI infrastructure. All WMI automation objects, methods and properties are implemented by the Wbemdisp.dll file. Note To run WMI, you must have administrator privileges. To access WMI through WMI scripting library, you need to perform three basic steps, which are common to most WMI scripts:
  4. 1. Connect to the Windows Management service. 2. Retrieve instances of WMI managed objects. 3. Call a method or access a managed object's property. Note To learn more about powerful WMI scripting capabilities, see the Microsoft Windows 2000 Professional Resource Kit or Microsoft Windows 2000 Server Resource Kit, where you can find more than 50 WMI-based scripts, enabling you to manage everything on the target computer, from boot configuration to user accounts. Enabling and Disabling System Restore on Windows XP Clients The example in Listing 15.3 automates the task of enabling or disabling System Restore on the specified drive. When it is begun, this code creates WshShell object, then requests user input, prompting the user if it is required to enable or disable System Restore (Fig. 15.8). To proceed further, the user must enter an appropriate text string (enable or disable) into the text field at the bottom of this dialog and click OK. Figure 15.8: Dialog box prompting the user to specify whether System Restore must be enabled or disabled Next, the script prompts the user to specify the drive on which it is necessary to take the specified action (Fig. 15.9). Specify the drive using the following format: :\, for example, C:\. Figure 15.9: Dialog box prompting the user to specify the drive on which the specified action must be taken
  5. The script runs and performs the specified action on the specified drive. After it is done, it displays a message box, informing the user of the result (Fig. 15.10). To make sure that the specified action was performed successfully, start the System applet in the Control Panel, go to the System Restore tab, and check if System Restore is actually turned off for the specified drive (Fig. 15.11). Figure 15.10: The message box informing the user of the result of the operation Figure 15.11: Use the System Restore Ul to check if System Restore is actually turned off for the drive you have specified when running the script Now let us consider the code that implements this series of actions (Listing 15.3). As was already mentioned, to use WMI scripting the code must connect to the Windows Management service, retrieve instances of the WMI-managed objects, and then call a method or access a managed object's property. In the example presented below, we connect to WMI using the WMI's moniker named winmgmts and SystemRestore class.
  6. Note A moniker is a standard COM mechanism for binding to a COM object. Detailed information on the WMI moniker syntax can be found at the following address: http://msdn.microsoft.com/library/psdk/wmisdk/scintro_6tpv.htm. Listing 15.3: VBScript Code for Enabling/Disabling System Restore on the Specified Drive ' Begin code for enabling or disabling System Restore Option Explicit Dim WSHShell, onoff, drive, SRP, eSRP, Result 'Creating WSHShell object Set WSHShell = CreateObject("WScript.Shell") 'Requesting user input onoff = inputbox ("Do you want to enable or disable System Restore?", "System Restore") Drive = inputbox ("Which Drive would you like to take action on? Must type in format 'c:\", "Drive to enable/disable") 'using WMI moniker and SystemRestore class to access WMI set SRP = GetObject("winmgmts:\\.\root\default:SystemRestore") If onoff = "enable" then eSRP = SRP.enable(dive) Result = MsgBox("System Restore is currenly" & _ vbNewLine & "enabled on the following drive:" & Drive, 64) end if If onoff = "disable" then eSRP = SRP.disable(drive) Result = MsgBox("System Restore is currenly" & _ vbNewLine & "disabled on the following drive:" & Drive, 64) end if ` End code Automatically Creating Restore Points on Windows XP Clients What else can we do with WMI and System Restore? Well, let us try to create a restore point automatically. Now, since we have already created several scripts, this is an easy task. Let us decide what our script must do. First, it must ask the user whether he or she wants to create a new restore point (Fig. 15.12).
  7. Figure 15.12: Dialog prompting the user to create a restore point Next, if the user clicks Yes, we must provide the user with the capability to enter the resource point description (Fig. 15.13). Figure 15.13: The dialog prompting the user to provide a description for the restore point to be created After the user provides a restore point description, we use WMI moniker and SystemRestore class to access WMI, and then create a new restore point using the description provided by the user. A very simple code performing these tasks is provided in Listing 15.4. Listing 15.4: Automatic Creation of the Restore Point Option Explicit Dim WSHShell, SRP, CSRP, description, Result Set WSHShell = CreateObject("WScript.Shell") Result = MsgBox("Do you want to create a Restore Point?", 36) If Result = 6 Then 'clicked yes description = inputbox ("Enter the Restore Point Description:", "Restore point to be created") 'use WMI moniker and SystemRestore class set SRP = getobject("winmgmts:\\.\root\default:Systemrestore") CSRP = SRP.createrestorepoint (description, 0, 100) end if ' End code
  8. After running this script, start System Restore and check if the restore point was actually created. The screenshot shown in Fig. 15.14 shows four test restore points, which I created automatically in the process of testing this script. Figure 15.14: The System Restore window displaying automatically created restore points Now, after carefully testing the script and making sure that it works, let us consider what practical use we can make of it. For example, wouldn't it be nice if after each successful logon, users (especially those who experiment with the registry) were prompted to create a restore point? As you remember, Windows XP is successfully loaded only after at least one user logs on to the system, and at that point the Clone control set is copied to the LastKnownGood configuration. Well, in my opinion, it makes sense if you also create a restore point at that time, just to be on the safe side. This small script can serve this purpose if you assign it as a logon script. To do so, just copy the script file to the %SystemRoot%\System32\GroupPolicy\User\Scripts\Logon directory (for standalone computers or in a workgroup environment) or to the \\Domain_controller\Sysvol\\Policies\\USER\Scripts\Logon directory (for domain environment), then start the Group Policy Object editor, and expand the console tree as shown in Fig. 15.15 (User Configuration | Windows Settings | Scripts). Double-click the Logon policy to open the Logon Properties window (Fig. 15.16), click the Add button and add our script to the list of available logon scripts.
  9. Figure 15.15: The Group Policy Object Editor window Figure 15.16: The Logon Properties window Now, each time the user logs on to the local computer, he or she will be prompted to create a restore point.
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

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