Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to manipulate result of reading keys from registry

Status
Not open for further replies.

JoeyDeRanged

Technical User
Sep 20, 2002
7
0
0
GB
I want to be able to run a different batch file according to which Windows registry key exists. Using Scanreg.exe I can search the registry and if the key exists it will spit it back at me, but how can I pump that output into a variable and compare it to a value, if it exists, execute option A, if not go scan for the existence of the the next key?

Any useful URLs to this kind of code would be of use.

thanks
 
You might want to use the Windows Script Host method REGREAD instead of scanreg. It'll return the key-value/value-name for the key you specify into a variable. However, if the key doesn't exist it'll error and you'll to add error checking for each key you search for. Here's a little bit of code to get you started...

' VBScript.
Set Sh = CreateObject("WScript.Shell")
key = "HKEY_CURRENT_USER\"
on error resume next
WScript.Echo Sh.RegRead(key & "WSHTest\")
if err.number <> 0 Then
MsgBox &quot;Error &quot;&CStr(Err.Number)
end if

It should error all the time 'cause who has a regkey called WSHTest? Replace it with a valid key and it will echo it.
 
Thanks for your input. Basically I'm still struggling as I'm not a programmer, just a beginner to this. I'm trying to automate changing mainly Windows NT machines from static IP addresses to DHCP, but have different registry places to amend depending on the network card installed (4 or 5 flavours).

' VBScript.
Set Sh = CreateObject(&quot;WScript.Shell&quot;)
key = &quot;HKLM\SYSTEM\CurrentControlSet\Services\&quot;
on error resume next
WScript.Echo Sh.RegRead(key & &quot;E1000\&quot;)

Then if that key (not value) exists run a batch file, if not continue to the next attempt to match ie

WScript.Echo Sh.RegRead(key & &quot;El90x1\&quot;)

If successful run another batchfile

Each batch file basically calls regedit /s then a registry merge file eg. E190x1.reg where

E190x1.reg contains:-

REGEDIT4

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\El90x1\Parameters\Tcpip]
&quot;EnableDHCP&quot;=dword:00000001
&quot;UseZeroBroadcast&quot;=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NetBT\Adapters\El90x1]
&quot;NameServer&quot;=&quot;&quot;
&quot;NameServerBackup&quot;=&quot;&quot;
&quot;DhcpNameServer&quot;=&quot;193.35.107.102&quot;
&quot;DhcpNameServerBackup&quot;=&quot;193.35.99.3&quot;


This could presumably be entered directly into the script using regwrite commands?
 
from the help file:

RegWrite Method
Sets the registry key or value named by strName.

object.RegWrite strName, anyValue [,strType]

Arguments
object

WshShell object.

strName

Key or value name to write.

anyValue

The value to write into the key or registry value.

strType

Optional. The data type for the value being stored in the registry.

Remarks
If strName ends with the backslash character (\), this method returns the key instead of the value. StrName must begin with one of following root key names:

Short Long
HKCU HKEY_CURRENT_USER
HKLM HKEY_LOCAL_MACHINE
HKCR HKEY_CLASSES_ROOT
HKEY_USERS
HKEY_CURRENT_CONFIG


RegWrite supports strType as REG_SZ, REG_EXPAND_SZ, REG_DWORD, and REG_BINARY. If another data type is passed as strType, RegWrite returns E_INVALIDARG.

RegWrite automatically converts anyValue to a string when strType is REG_SZ or REG_EXPAND_SZ. If strType is REG_DWORD, anyValue is converted to an integer. If strType is REG_BINARY, anyValue must be an integer.

Example
The following example writes a value and key entry into the registry:

Set WshShell = WScript.CreateObject(&quot;WScript.Shell&quot;)
WshShell.RegWrite &quot;HKCU\ScriptEngine\Value&quot;, &quot;Some string value&quot;
WshShell.RegWrite &quot;HKCU\ScriptEngine\Key\&quot;, 1 ,&quot;REG_DWORD&quot;
See Also ===============
Security Forums
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top