views:

87

answers:

3

Hi All,

Is there a way to search the Registry for a specific key using Windows Scripting Host?

I'm using JavaScript (Jscript/VBScript?) to do so, and the msdn Library doesn't mention any such method: http://msdn.microsoft.com/en-us/library/2x3w20xf(v=VS.85).aspx

Thanks,


So here's an update to the problem:

The problem is a bit more complicated than a direct registry search. I have to look through the installed products on a windows box, to find a specific product entry that i want to delete. The registry path is defined as:

HKEY_LOCAL_MACHINE\Software\Microsoft...\Products.

Within the Products key, the installed products are listed, but their keys are defined as hash codes. Within the product keys are other keys with defined names and defined values. I want to be able to search on the latter keys and values. How can I do that, by-passing the unknown hash codes?

For example, I need to find a product with DisplayVersion key = 1.0.0. The path to that key is:

HKLM\Software\Microsoft\Windows\CurrentVersion\Installer\UserData\Products\A949EBE4EED5FD113A0CB40EED7D0258\InstallProperties\DisplayVersion.

How can I either pick up, or avoid writing, the product key: A949EBE4EED5FD113A0CB40EED7D0258 ??

+1  A: 

It is highly unlikely that you would be able to search the registry using javascript, as such an operation would be considered unsafe and insecure.

Robert Harvey
He's not talking about doing it from a browser.
Pointy
A: 

http://code.google.com/p/jslibs/

if you don't find it there, you have to implement it yourself

mono
Those are irrelevant.
Pointy
+3  A: 

Assuming you're using JScript via the Windows Scripting Host (and not JavaScript from a browser) you can get the value of a specific key using the WScript.RegRead method:

// MyScript.js
var key = 'HKEY_CURRENT_USER\\SessionInformation\\ProgramCount'
  , wsh = WScript.CreateObject('WScript.Shell')
  , val = wsh.RegRead(key);
WScript.Echo('You are currently running ' + val + ' programs.');

If you actually need to search for a key or value based on some conditions rather than a known registry key then you can to implement your own recursive search algorithm where registry values of type "REG_SZ" are leaf nodes.

As an exercise to get more familiar with JScript on the Windows Scripting Host, I've made a small interface to the registry that does exactly this. The example included in the project shows how to perform such a registry search in a WSF script:

<job id="FindDisplayVersions">
  <script language="jscript" src="../registry.js"/>
  <script language="jscript">
    // Search the registry and gather 20 DisplayVersion values.
    var reg = new Registry()
      , rootKey = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products'
      , keyRegex = /Products\\(.*?)\\InstallProperties\\DisplayVersion$/
      , valRegex = /^1\./
      , maxResults = 20
      , uids = [];
    reg.find(rootKey, function(path, value) {
      var keyMatch = keyRegex.exec(path);
      if (keyMatch) {
        if (valRegex.exec(value)) {
          uids.push(keyMatch[1] + '\t=\t' + value);
          if (uids.length >= maxResults) { return false; } // Stop searching
        }
      }
      return true; // Keep searching.
    });
    WScript.Echo(uids.join("\n"));
  </script>
</job>

Note that, as @Robert Harvey points out, this could take a really long time if the root key is too deeply connected. Simple testing takes only a few seconds on the key I chose but your mileage may vary; of course, no warranty or fitness for a purpose, don't blame me if your computer blows up.

maerics
It will take forever.
Robert Harvey
@Robert - no doubt! Hopefully the root node isn't too deeply connected...
maerics
Thanks to all. So the problem is a bit more complicated than a direct registry search. I have to look through the installed products on a windows box, to find a specific product entry that i want to delete. The registry path is defined as: HKEY_LOCAL_MACHINE\Software\Microsoft\...\Products\. Within the Products key, the installed products are listed, but their keys are defined as hash codes. Within the product keys are other keys with defined names and defined values. I want to be able to search on the latter keys and values. How can I do that, by-passing the unknown hash codes?
rkhj
For example, I need to find a product with DisplayVersion key = 1.0.0. The path to that key is: HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\Products\\A949EBE4EED5FD113A0CB40EED7D0258\\InstallProperties\\DisplayVersion. How can I either pick up, or avoid writing, the product key: A949EBE4EED5FD113A0CB40EED7D0258 ??
rkhj
@rkhj, you should update the question with this information instead of posting as a comment, this way it's clearly part of the question and not just a comment to my solution...
maerics