在注册表中搜索密钥 - JScript

发布于 2024-09-01 07:51:54 字数 827 浏览 1 评论 0原文

有没有办法使用 Windows 脚本主机在注册表中搜索特定密钥?

我使用 JavaScript (Jscript/VBScript?)来执行此操作,并且 msdn 库没有提及任何此类方法:http://msdn.microsoft.com/en-us/library/2x3w20xf(v=VS.85).aspx< /a>

谢谢,


所以这里是问题的更新:

这个问题比直接注册表搜索要复杂一些。我必须查看 Windows 盒子上已安装的产品,找到我想要删除的特定产品条目。注册表路径定义为:

HKEY_LOCAL_MACHINE\Software\Microsoft...\Products。

在产品密钥中,列出了已安装的产品,但它们的密钥定义为哈希码。产品密钥内还有具有定义名称和定义值的其他密钥。我希望能够搜索后面的键和值。我怎样才能做到这一点,绕过未知的哈希码?

例如,我需要查找 DisplayVersion key = 1.0.0 的产品。该密钥的路径为:

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

我如何获取或避免写入产品密钥:A949EBE4EED5FD113A0CB40EED7D0258?

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 ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断桥再见 2024-09-08 07:51:55

假设您通过 Windows 脚本主机使用 JScript(而不是来自浏览器的 JavaScript),您可以使用 WScript.RegRead 方法:

// 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.');

如果您确实需要根据某些条件而不是某个值来搜索键或值如果已知注册表项,则您可以实现自己的递归搜索算法,其中“REG_SZ”类型的注册表值是叶节点。

作为更熟悉 Windows 脚本主机上的 JScript 的练习,我制作了一个注册表的小界面 正是这样做的。该项目中包含的示例展示了如何在 WSF 脚本中执行此类注册表搜索:

<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>

请注意,正如 @Robert Harvey 指出的那样,如果根键连接太深,这可能需要很长时间。对我选择的钥匙进行简单测试只需几秒钟,但您的里程可能会有所不同;当然,没有任何保证或适用性,如果您的计算机爆炸了,请不要责怪我。

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.

帥小哥 2024-09-08 07:51:55

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

(如果您不这样做)在那里找到它,你必须自己实现它

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文