Registry key reading 
Author Message
 Registry key reading

I'm trying to get a listing of all the installed software by extracting the
"DisplayName" from the subkeys of
HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall using the following
code (watch for broken lines)...

'***** Start code sample *****
Option Explicit

Const TempFldr = "C:\Temp"
Const RegFile = "C:\Temp\HKLM_Uninstall_Key.txt"

Dim fso, oShell, oInStream
Dim sLineIn, i

Set fso = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("Wscript.Shell")

'Export Uninstall Registry key
If NOT fso.FolderExists(TempFldr) Then fso.CreateFolder(TempFldr)
oShell.Run "Regedit /s /e " & RegFile & "
""HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"""
WScript.Sleep 1000

'Evaluate output to find DisplayName
Set oInStream = fso.OpenTextFile(RegFile, 1)
i = 1
Do Until oInStream.AtEndOfStream
   sLineIn = oInStream.ReadLine
   WScript.Echo i & " " & sLineIn
'   If InStr(sLineIn, "DisplayName") <> 0 Then
'      WScript.Echo sLineIn
'   End If
   i = i + 1
   If i = 11 Then Exit Do
Loop
'***** End code sample *****

It creates the file and I can read it with Notepad, but sLineIn contains
either nothing or garbage when I look at it.  Why?  Is there a better way to
read all the subkeys of a key like I'm trying to do?

Thanks,

Tom



Sat, 01 Jan 2005 02:27:42 GMT  
 Registry key reading
Corrections added inline in the script:

Quote:

> I'm trying to get a listing of all the installed software by extracting the
> "DisplayName" from the subkeys of
> HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall using the following
> code (watch for broken lines)...

> '***** Start code sample *****
> Option Explicit

> Const TempFldr = "C:\Temp"
> Const RegFile = "C:\Temp\HKLM_Uninstall_Key.txt"

> Dim fso, oShell, oInStream
> Dim sLineIn, i

> Set fso = CreateObject("Scripting.FileSystemObject")
> Set oShell = CreateObject("Wscript.Shell")

> 'Export Uninstall Registry key
> If NOT fso.FolderExists(TempFldr) Then fso.CreateFolder(TempFldr)
> oShell.Run "Regedit /s /e " & RegFile & "
> ""HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"""

2 things:
1)
You need a space between RegFile and HKEY_LOCAL_MACHINE.
2)
You need to make the Run method wait for regedit.exe to finish by setting the
bWaitOnReturn flag to True.

Fixed Run command:

oShell.Run "Regedit /s /e " & RegFile _
  & " ""HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\" _
  & "CurrentVersion\Uninstall""", 1, True

Quote:

> WScript.Sleep 1000

Because of the added bWaitOnReturn flag, the WScript.Sleep can be deleted.

Quote:
> 'Evaluate output to find DisplayName
> Set oInStream = fso.OpenTextFile(RegFile, 1)

Here is your main problem (the garbled output).
You do not specify the format for the file when you open the file with
OpenTextFile. WSH will then default to ASCII, while Regedit on Win2k/WinXP saves
the file in Unicode format. Trying to read an Unicode file as ASCII will just
end up as garbled text.

The best think here is to use the TristateUseDefault mode (-2) on the format for
OpenTextFile, it will correspond to the output format from Regedit.exe on all
OS's (a.k.a. ASCII on Win9x/NT 4.0 and Unicode on Win2k/WinXP).

Set oInStream = fso.OpenTextFile(RegFile, 1, False, -2)

Quote:
> i = 1
> Do Until oInStream.AtEndOfStream
>    sLineIn = oInStream.ReadLine
>    WScript.Echo i & " " & sLineIn
> '   If InStr(sLineIn, "DisplayName") <> 0 Then
> '      WScript.Echo sLineIn
> '   End If
>    i = i + 1
>    If i = 11 Then Exit Do
> Loop
> '***** End code sample *****

> It creates the file and I can read it with Notepad, but sLineIn contains
> either nothing or garbage when I look at it.  Why?

Anwered inline in code.

Quote:
>  Is there a better way to
> read all the subkeys of a key like I'm trying to do?

You can use WMI and it's StdRegProv class instead as long as all your computers
got WMI installed. WMI comes preinstalled on Win2k/WinXP/WinME, can be installed
on NT4/Win9x.

Platform SDK: Windows Management Instrumentation
StdRegProv
http://msdn.microsoft.com/library/en-us/wmisdk/wmi/stdregprov.asp

Here is an example that gets a list of installed software:

sComputerName = "."  ' use "." for local computer

WScript.Echo InstalledApplications(sComputerName)

Function InstalledApplications(node)
 'Torgeir Bakken's get installed applications routine
 Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
 Set oRegistry = _
  GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
  & node & "/root/default:StdRegProv")
 sBaseKey = _
  "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
 iRC = oRegistry.EnumKey(HKLM, sBaseKey, arSubKeys)
 For Each sKey In arSubKeys
  iRC = oRegistry.GetStringValue( _
   HKLM, sBaseKey & sKey, "DisplayName", sValue)
  If iRC <> 0 Then
   oRegistry.GetStringValue _
    HKLM, sBaseKey & sKey, "QuietDisplayName", sValue
  End If
  If sValue <> "" Then
   InstalledApplications = _
    InstalledApplications & sValue & vbCrLf
  End If
 Next
End Function

--
torgeir



Sat, 01 Jan 2005 03:40:57 GMT  
 Registry key reading

Quote:

> I'm trying to get a listing of all the installed software by
> extracting the "DisplayName" from the subkeys of
> HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall using the
> following code (watch for broken lines)...

    ...snip...

> It creates the file and I can read it with Notepad, but sLineIn
> contains either nothing or garbage when I look at it.  Why?  Is there
> a better way to read all the subkeys of a key like I'm trying to do?

Google Search: installedapplications group:*.scripting author:Torgeir Bakken
http://groups.google.com/groups?selm=3D2DDF6B.A5FB775D%40hydro.com

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US
--



Sat, 01 Jan 2005 03:49:37 GMT  
 Registry key reading
Thanks for the help, Torgeir.  Both solutions worked great.

The WMI approach is certainly more elegant.  My only problem with that is
getting WMI on all our NT PCs.  Right now we're probably 60% NT4 and 40%
2000.  We put WSH on all the NT PCs when we pushed IE 5.5 to everyone, but
we didn't do WMI at that time.  Hindsight being 20/20 it probably would have
been a good thing to do at that time.

How can I get WMI on all those PCs (8000)?  Can I install it via the user's
logon script?  Will it require elevated rights to install (most of our users
are USERs)?

Thanks,

Tom



Sat, 01 Jan 2005 21:49:07 GMT  
 Registry key reading

Quote:

> The WMI approach is certainly more elegant.  My only problem with that is
> getting WMI on all our NT PCs.  Right now we're probably 60% NT4 and 40%
> 2000.  We put WSH on all the NT PCs when we pushed IE 5.5 to everyone, but
> we didn't do WMI at that time.  Hindsight being 20/20 it probably would have
> been a good thing to do at that time.

> How can I get WMI on all those PCs (8000)?  Can I install it via the user's
> logon script?  Will it require elevated rights to install (most of our users
> are USERs)?

Yes, it will need elevated rights. The install can be run unattended/silent as
well.

--
torgeir



Sat, 01 Jan 2005 21:52:47 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Read Registry Keys and Trees / API Call for Registry access

2. REGISTRY How can I read a registry key?

3. Read Registry key via JScript

4. Reading Registry keys??

5. reading a registry key

6. Reading Binary Registry Keys

7. Reading key in registry

8. Read/write registry key using .asp or .hta

9. Reading registry keys in vbscript from ASP page

10. read/set registry key

11. How to read registry key containing many string values

12. read (all) subkeys of a given registry key ?

 

 
Powered by phpBB® Forum Software