Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how do I query the registry subkeys , values and data in

46 views
Skip to first unread message

gs

unread,
Mar 31, 2006, 2:23:22 AM3/31/06
to
how do I selectively print data values and subkey of HKEY_CLASSES_ROOT,
based on values or data containing some specific words? Do I use WMI?


JTW

unread,
Mar 31, 2006, 8:49:51 AM3/31/06
to
Are you asking for a way to search the registry for a specific pattern
and then return results?

TDM

unread,
Mar 31, 2006, 4:08:18 PM3/31/06
to

"gs" <g...@dontMail.telus> wrote in message
news:%23O2tnOJ...@TK2MSFTNGP12.phx.gbl...

> how do I selectively print data values and subkey of HKEY_CLASSES_ROOT,
> based on values or data containing some specific words? Do I use WMI?
>

You can certainly do this with WMI, but beware on remote
systems, this can be very slow, especially given the size of the
HIVE you have chosen.

In my experience, aside from this taking a long time, WMI can
sometimes either take forever to even connect to the remote
system, or take forever to never connect to the remote system
without really giving you much to go on. I have seen stuff in
google about setting a WMI timeout, ie:

<code>
Set oLocator = CreateObject("WbemScripting.SWbemLocator")
Set oWMIService = oLocator.ConnectServer(sMachine, _
"root\cimv2" ,sUsername,sPassword,,,wbemConnectFlagUs
eMaxWait)
</code>

But, I have never personally worked with this. This is only
supported on XP/Server2003 if I am not mistaken.

Then, to enumerate the entire hive, you will need to make
recursive calls untill all sub items have been enumerated. I
have done this in a script to migrate registry data from one
system to another, if interested, I can post.

Just my $.02

TDM


gs

unread,
Apr 2, 2006, 11:56:43 AM4/2/06
to
yes
"JTW" <News...@Dx21.com> wrote in message
news:%23eic8nM...@TK2MSFTNGP12.phx.gbl...

gs

unread,
Apr 2, 2006, 12:08:48 PM4/2/06
to
it will help. I can then use results of the enumeration to select for a
pattern.

I am looking for certain pattern to migrate to a new system. I envision
the script to be in 2 parts:
locally locating and outputting the entries I need from the old system
as is
translating to new location and generate new entries for the new PC -
this part can be done on the new faster PC

I tried simple things from cmd.exe:
reg query hkcr /s | findstr /i "somepattern" > file1 ' to assess
extent and validate searching file2
reg query hkcr /s > file2
But I find the result are unwieldy

BTW, how do I add something to HKCR? I could not even do that with an
local ID with admin power.

"TDM" <rpu...@gmail.com> wrote in message
news:uErh9cQ...@TK2MSFTNGP09.phx.gbl...

TDM

unread,
Apr 3, 2006, 11:06:44 AM4/3/06
to

"gs" <g...@dontMail.telus> wrote in message
news:eLubu9mV...@TK2MSFTNGP14.phx.gbl...

> it will help. I can then use results of the enumeration to select for a
> pattern.
<snip>

> BTW, how do I add something to HKCR? I could not even do that with an
> local ID with admin power.
<snip>

gs,

I have no personal experiencs with HKCR as far as accessing via
script, so I wont be much help to you here. However, you may
with to try with the following function, you never know.

My disclaimer, I snagged these function from a very large data
migration script, I may have left out something, although I hope
not. I also pruned out a bunch of stuff related to logging to file
and dialog to the user, I hope I did not remove too much and
break the beast.

This function will use WMI to connect to a remote system and
migrate a registry hive recursively to the local system. I am sure
you know this but you need admin rights to the remote system.

WATCH FOR LINE WRAP

Call the function passing in the hostname to connect to, the hive
you wish to start at, and the Key you wish to migrate, ex :

strErr = regParse(strHostName, "HKEY_CURRENT_USER",
"Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem")

This example will migrate the info needed to configure outlook, a big
time save if the user hadstons of personal storage folders. You also
need to make sure the PST's are in the same location as the old system.

The 2 functions needed :

Function regParse(fromSys, regHive, regKey)
' This function will evalutate a registry key on the remote system and
' transfer those values to the local system(I hope !)
' Only supported on Win2K and beyond(requires WMI).
'
' Required inputs
'
' fromSys as string, the hostname of the remote system that you would
' like to transfer registry values from.
'
' regHive as string, the ROOT hive to start from
'
' regKey as string, the starting path in the registry that you would
' like imported.
'
' Required functions
'
' regDiver(oReg, oLocalReg, bRegHive, strRegHive, strKeyPath)

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005
Const HKEY_DYN_DATA = &H80000006

Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_MULTI_SZ = 7

Dim strComputer
Dim bRegHive
Dim strRegHive
Dim strKeyPath
Dim subkey
Dim lrc
Dim oLocalReg
Dim strLocalComputer
Dim oLastError

Err.Clear
On Error Resume Next

strComputer = fromSys
strLocalComputer = "."

If Instr(1, LCase(regHive), "root", 1) <> 0 Then
strRegHive = "HKEY_CLASSES_ROOT"
bRegHive = HKEY_CLASSES_ROOT
ElseIf Instr(1, LCase(regHive), "current_user", 1) <> 0 Then
strRegHive = "HKEY_CURRENT_USER"
bRegHive = HKEY_CURRENT_USER
ElseIf Instr(1, LCase(regHive), "machine", 1) <> 0 Then
strRegHive = "HKEY_LOCAL_MACHINE"
bRegHive = HKEY_LOCAL_MACHINE
ElseIf Instr(1, LCase(regHive), "users", 1) <> 0 Then
strRegHive = "HKEY_USERS"
bRegHive = HKEY_USERS
ElseIf Instr(1, LCase(regHive), "config", 1) <> 0 Then
strRegHive = "HKEY_CURRENT_CONFIG"
bRegHive = HKEY_CURRENT_CONFIG
ElseIf Instr(1, LCase(regHive), "data", 1) <> 0 Then
strRegHive = "HKEY_DYN_DATA"
bRegHive = HKEY_DYN_DATA
Else
regParse = False
Exit Function
End If

Set oLocalReg =
GetObject("winmgmts:{impersonationLevel=impersonate,authenticationLevel=Default}!\\"
& strLocalComputer & "\root\default:StdRegProv")

If Err.Number <> 0 Then
Set oLastError = CreateObject("WbemScripting.SWbemLastError")
WScript.Echo "WMI Error : " & oLastError.getObjectText_(1)
Set oLastError = Nothing
regParse = Err.Description & ":" & Err.Number & ":" &
oLastError.getObjectText_(1)
Set oReg = Nothing
Set oLocalReg = Nothing
Exit Function
End If

Set oReg =
GetObject("winmgmts:{impersonationLevel=impersonate,authenticationLevel=Default}!\\"
& strComputer & "\root\default:StdRegProv")

If Err.Number <> 0 Then
Set oLastError = CreateObject("WbemScripting.SWbemLastError")
WScript.Echo "WMI Error : " & oLastError.getObjectText_(1)
Set oLastError = Nothing
regParse = Err.Description & ":" & Err.Number & ":" &
oLastError.getObjectText_(1)
Set oReg = Nothing
Set oLocalReg = Nothing
Exit Function
End If

strKeyPath = regKey
lrc = oReg.EnumKey(bRegHive, strKeyPath, arrSubKeys)

If IsArray(arrSubkeys) And lrc = 0 Then
For Each subkey In arrSubKeys
oLocalReg.CreateKey bRegHive, strKeyPath & "\" & subkey
If subkey <> "" Then
Call regDiver(oReg, oLocalReg, bRegHive, strRegHive, strKeyPath)
End If
Next
regParse = True
ElseIf lrc = 0 Then
oLocalReg.CreateKey bRegHive, strKeyPath & "\" & subkey
Call regDiver(oReg, oLocalReg, bRegHive, strRegHive, strKeyPath)
regParse = True
Else
regParse = lrc & ":" & Err.Description & ":" & Err.Number
End If

Set oReg = Nothing
Set oLocalReg = Nothing

End Function

'******************************************************************************

Function regDiver(oReg, oLocalReg, bRegHive, strRegHive, strKeyPath)
' This function will traverse a registry path via recursive
' calls. It will also iterate and parse registry keyNames
' and keyValues
'
' Required inputs
'
' oReg : Type object, an object reference to
' WMI:StdRegProv on the remote system
'
' oLocalReg : Type object, an object reference to
' WMI:StdRegProv on the local system

' strRegHive : Type String, the ROOT Registry
' Hive to start from
'
' strKeyPath : Type String, the registry path
' starting at the hive specified in strRegHive
'
Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_MULTI_SZ = 7

Dim i
Dim ary
Dim sMsg
Dim regVal
Dim bVal, bValues, bVals
Dim sVal, sValue
Dim dValue
Dim sValues
Dim lrc
Dim arrValueNames, arrValueTypes

i = 0

On Error Resume Next

lrc = oReg.EnumValues(bRegHive, strKeyPath, arrValueNames, arrValueTypes)

If IsArray(arrValueNames) And lrc = 0 Then
For Each ary In arrValueNames
Select Case arrValueTypes(i)
Case REG_SZ
regVal = oReg.GetStringValue(bRegHive, strKeyPath, ary, sValue)
oLocalReg.SetStringValue bRegHive, strKeyPath, ary, sValue
Case REG_EXPAND_SZ
regVal = oReg.GetExpandedStringValue(bRegHive, strKeyPath, ary, sValue)
oLocalReg.SetExpandedStringValue bRegHive, strKeyPath, ary, sValue
Case REG_BINARY
regVal = oReg.GetBinaryValue(bRegHive, strKeyPath, ary, bValues)
If IsArray(bValues) Then
oLocalReg.SetBinaryValue bRegHive, strKeyPath, ary, bValues
End If
Case REG_DWORD
regVal = oReg.GetDWORDValue(bRegHive, strKeyPath, ary, dValue)
oLocalReg.SetDWORDValue bRegHive, strKeyPath, ary, dValue
Case REG_MULTI_SZ
regVal = oReg.GetMultiStringValue(bRegHive, strKeyPath, ary, sValues)
If IsArray(sValues) Then
For Each sVal In sValues
regVal = oReg.GetDWORDValue(bRegHive, strKeyPath, ary, dValue)
oLocalReg.SetMultiStringValue bRegHive, strKeyPath, ary, dValue
Next
End If
Case Else
sMsg = ary & " = " & "Unknown" & vbCrLf
End Select
i = i + 1
Next
End If

lrc = oReg.EnumKey(bRegHive, strKeyPath, arrSubKeys)

If IsArray(arrSubKeys) And lrc = 0 Then ' return value of 0 indicates
success
For each ary in arrSubKeys
If ary <> "" Then ' "Default" value is empty string, let's ignore
oLocalReg.CreateKey bRegHive, strKeyPath & "\" & ary
Call regDiver(oReg, oLocalReg, bRegHive, strRegHive, strKeyPath & "\" &
ary)
End If
Next
End If

End Function


0 new messages