234
Part
II
Windows VBScript and JScript
JScript
readkey.js
var ws = WScript.CreateObject (“WScript.Shell”)
v=ws.RegRead(“HKLM\\SYSTEM\\CurrentControlSet\\Services\\WINS\\Parameters\\
DbFileNm”)
WScript.Echo(v)
The
RegRead
method only supports the standard data types:
REG_SZ
,
REG_EXPAND_SZ
,
REG_
MULTI_SZ
,
REG_DWORD
, and
REG_BINARY
. If the value contains another data type, the method
returns
DISP_E_TYPEMISMATCH
.
Writing registry keys and values
Creating keys and writing registry values is a bit different than reading key values. To write keys
and value entries to the registry, use the
RegWrite
method of the
WshShell
object. This method
expects to be passed the key name as well as the value you want to set. You can also set an optional
parameter that specifies the value type. If you don’t set the type parameter, the value is set as a
string of type
REG_SZ
. If you set the value type, the type must be one of the following:
REG_SZ
,
REG_EXPAND_SZ
,
REG_DWORD
, or
REG_BINARY
.
Some value types are converted automatically to the appropriate format. With
REG_SZ
and
REG_
EXPAND_SZ
,
RegWrite
automatically converts values to strings. With
REG_DWORD
, values are con-
verted to integers in hexadecimal format. However,
REG_BINARY
must be set as integers. If you set
an incorrect data type or an incorrect value,
RegWrite
returns
E_INVALIDARG
.
You can use
RegWrite
to update existing registry keys and values as well as to create new keys and
values. If the path ends with a slash (or double slash for JScript), the entry is written as a key. Otherwise,
the entry is written as a value entry. Listing 12-2 changes the value entry for the
DbFileNm
key and
then confirms the change by reading the new value.
LISTING 12-2
Modifying an Existing Key
VBScript
modkey.vbs
Dim Path
Path = “HKLM\SYSTEM\CurrentControlSet\Services\WINS\Parameters\”
Set ws = WScript.CreateObject(“WScript.Shell”)
o=ws.RegWrite(Path & “DbFileNm”, “%windir%\system32\wins.mdb”, “REG_EXPAND_SZ”)
v=ws.RegRead(Path & “DbFileNm”)
WScript.Echo v
LISTING 12-1
(continued)
86804c12.indd 234
86804c12.indd 234
1/21/09 1:26:54 PM
1/21/09 1:26:54 PM
235
Working with the Windows Registry and Event Logs
12
JScript
modkey.js
var Path
Path = “HKLM\\SYSTEM\\CurrentControlSet\\Services\\WINS\\Parameters\\”
var ws = WScript.CreateObject(“WScript.Shell”)
o=ws.RegWrite(Path + “DbFileNm”,”%windir%\\system32\\wins.mdb”,
“REG_EXPAND_SZ”)
v=ws.RegRead(Path + “DbFileNm”)
WScript.Echo(v)
If you change the settings of a Windows service, you will need to restart the service
before the changes take effect. If the service won’t start after you’ve made changes, you
should change the key values back to their original settings.
Creating new keys
When you create new keys, you don’t have to worry about creating the tree structure that may be
associated with the key. The registry automatically creates additional folders as necessary.
Usually, you’ll want to add new keys to the
HKEY_CURRENT_USER
root key. For example, you can
create a new key for Windows scripts called
HKEY_CURRENT_USER\WSHBible
and then add values
to it. Because these values are stored in the current user’s profile, they are persistent and aren’t
destroyed when the user logs out. This makes it possible to retain values across multiple user
sessions.
Listing 12-3 shows an example of creating registry keys and assigning values to the keys. The key
created is
HKEY_CURRENT_USER\WSHBible
. The values associated with the key are named
Author
and
Comments
.
LISTING 12-3
Creating Registry Keys and Values
VBScript
Createregkey.vbs
Set ws = WScript.CreateObject(“WScript.Shell”)
val = ws.RegWrite(“HKCU\WSHBible\Author”,”William Stanek”)
val = ws.RegWrite(“HKCU\WSHBible\Comments”,”Covers Windows Script Host”)
JScript
createregkey.js
var ws = WScript.CreateObject(“WScript.Shell”)
val = ws.RegWrite(“HKCU\\WSHBible\\Author”,”William Stanek”)
val = ws.RegWrite(“HKCU\\WSHBible\\Comments”,”Covers Windows Script Host”)
TIP
TIP
86804c12.indd 235
86804c12.indd 235
1/21/09 1:26:54 PM
1/21/09 1:26:54 PM
236
Part
II
Windows VBScript and JScript
Deleting registry keys and values
You delete registry keys using the
RegDelete
method of the
WshShell
object. The only argument
for the method is the full path for the key or the value you want to delete. When you delete a key, the
path should end with a slash, like this:
HKCU\WSHBible\
. When you delete a key value, the slash
isn’t necessary. For example, you can delete the
Author
value using
HKCU\WSHBible\Author
as
the argument to
RegDelete
.
Listing 12-4 shows an example of how you can delete the
Author
and
Comment
values created in
the previous section. The example doesn’t delete the
HKEY_CURRENT_USER\WSHBible
key.
LISTING 12-4
Deleting Registry Values
VBScript
deleteregkey.vbs
Set ws = WScript.CreateObject(“WScript.Shell”)
val = ws.RegDelete(“HKCU\WSHBible\Author”)
val = ws.RegDelete(“HKCU\WSHBible\Comments”)
JScript
deleteregkey.js
var ws = WScript.CreateObject(“WScript.Shell”)
val = ws.RegDelete(“HKCU\\WSHBible\\Author”)
val = ws.RegDelete(“HKCU\\WSHBible\\Comments”)
If you want to delete the
HKEY_CURRENT_USER\WSHBible
key, you change the listing as follows:
VBScript
Set ws = WScript.CreateObject(“WScript.Shell”)
val = ws.RegDel(“HKCU\WSHBible\”)
JScript
var ws = WScript.CreateObject(“WScript.Shell”)
val = ws.RegDel(“HKCU\\WSHBible\\”)
When you delete a key, you permanently delete all of the values associated with the key
as well.
Reconfiguring network services through the registry
To better understand how the registry controls system and network settings, let’s take a detailed look
at how you can manage WINS and DHCP through the Windows Registry.
NOTE
NOTE
86804c12.indd 236
86804c12.indd 236
1/21/09 1:26:54 PM
1/21/09 1:26:54 PM
Dostları ilə paylaş: |