将代码从 vbscript 转换为 Jscript?

发布于 2024-10-30 16:09:48 字数 1350 浏览 0 评论 0原文

如何将以下 VBScript 代码转换为 JScript 以用于获取所有用户的用户配置文件路径?

Set oWshNet = CreateObject("Wscript.Network")
sComputer = oWshNet.ComputerName
'For remote computer
'sComputer = "some name or IP"
Const HKLM = &H80000002

sProfileRegBase = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" 
Set oReg = GetObject("WinMgmts:{impersonationLevel=impersonate}!//" _ 
                & sComputer & "/root/default:StdRegProv")
Set oWMI = GetObject("WinMgmts:{impersonationLevel=impersonate}!//" _ 
                & sComputer & "/root/cimv2")
Set colItems = oWMI.ExecQuery _ 
                ("Select Name,SID from Win32_UserAccount WHERE Domain = '" _ 
                & sComputer & "'",,48)
For Each oItem In colItems
  sAddInfo = ""
  Wscript.Echo "User name: " & oItem.Name & sAddInfo
  oReg.GetExpandedStringValue HKLM, sProfileRegBase& "\" & oItem.SID, _ 
                  "ProfileImagePath", sProfilePath
  If IsNull(sProfilePath) Then
    sProfilePath = "(none defined)"
  End If <br>
  Wscript.Echo "Profile path: " & sProfilePath
  Wscript.Echo   ' blank line
Next

我在转变方面取得了部分成功,但坚持了两件事。

  1. 请确认我的使用方式是否正确并且与给出的相同在代码中。如果不是,请建议正确的用法。

  2. JScript 中 GetExpandedStringValue 的等效项是什么?如果没有,在获取值之前验证注册表项是否存在的更好方法是什么?

How do I convert the following VBScript code to JScript which is used for getting the user profile paths for all users?

Set oWshNet = CreateObject("Wscript.Network")
sComputer = oWshNet.ComputerName
'For remote computer
'sComputer = "some name or IP"
Const HKLM = &H80000002

sProfileRegBase = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" 
Set oReg = GetObject("WinMgmts:{impersonationLevel=impersonate}!//" _ 
                & sComputer & "/root/default:StdRegProv")
Set oWMI = GetObject("WinMgmts:{impersonationLevel=impersonate}!//" _ 
                & sComputer & "/root/cimv2")
Set colItems = oWMI.ExecQuery _ 
                ("Select Name,SID from Win32_UserAccount WHERE Domain = '" _ 
                & sComputer & "'",,48)
For Each oItem In colItems
  sAddInfo = ""
  Wscript.Echo "User name: " & oItem.Name & sAddInfo
  oReg.GetExpandedStringValue HKLM, sProfileRegBase& "\" & oItem.SID, _ 
                  "ProfileImagePath", sProfilePath
  If IsNull(sProfilePath) Then
    sProfilePath = "(none defined)"
  End If <br>
  Wscript.Echo "Profile path: " & sProfilePath
  Wscript.Echo   ' blank line
Next

I was partially succeeded in converting but stuck at 2 things.

  1. Please confirm whether my usage of oReg = GetObject("WinMgmts:\\\\.\\root\\default:StdRegProv"); is correct and is same as the one that was given in the code. If not please suggest the right usage.

  2. What is the equivalent for GetExpandedStringValue in JScript? If there is none, what is the better way to validate if a registry key exists before getting the value?

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

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

发布评论

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

评论(3

绝對不後悔。 2024-11-06 16:09:48

这是一个示例解决方案:(来自 http://www.windowsitpro.com/content /content/93402/Listing_05.txt

// GetSystemPath.js

var HKEY_LOCAL_MACHINE = 0x80000002;
var ENVIRONMENT_SUBKEY = "SYSTEM\\CurrentControlSet\\Control"
  + "\\Session Manager\\Environment";

var computer, regprov, method, inparams, outparams, systempath;

// CALLOUT A
// Step 1: Get an instance of the WMI object.
computer = ".";
regprov = GetObject("winmgmts:{impersonationlevel=impersonate}!//"
  + computer + "/root/default:StdRegProv");
// END CALLOUT A

// CALLOUT B
// Step 2: Create an InParameters object for the method.
method = regprov.Methods_.Item("GetExpandedStringValue");
inparams = method.InParameters.SpawnInstance_();
// END CALLOUT B

// CALLOUT C
// Step 3: Set the InParameters object's properties.
inparams.hDefKey = HKEY_LOCAL_MACHINE;
inparams.sSubKeyName = ENVIRONMENT_SUBKEY;
inparams.sValueName = "Path";
// END CALLOUT C

// CALLOUT D
// Step 4: Call ExecMethod_ to return an OutParameters object.
outparams = regprov.ExecMethod_(method.Name, inparams);
// END CALLOUT D

// CALLOUT E
// Step 5: The OutParameters object contains the method's results.
if (outparams.ReturnValue == 0) {
  systempath = outparams.sValue;
  WScript.Echo(systempath);
}
// END CALLOUT E

Here is a sample solution: (from http://www.windowsitpro.com/content/content/93402/Listing_05.txt)

// GetSystemPath.js

var HKEY_LOCAL_MACHINE = 0x80000002;
var ENVIRONMENT_SUBKEY = "SYSTEM\\CurrentControlSet\\Control"
  + "\\Session Manager\\Environment";

var computer, regprov, method, inparams, outparams, systempath;

// CALLOUT A
// Step 1: Get an instance of the WMI object.
computer = ".";
regprov = GetObject("winmgmts:{impersonationlevel=impersonate}!//"
  + computer + "/root/default:StdRegProv");
// END CALLOUT A

// CALLOUT B
// Step 2: Create an InParameters object for the method.
method = regprov.Methods_.Item("GetExpandedStringValue");
inparams = method.InParameters.SpawnInstance_();
// END CALLOUT B

// CALLOUT C
// Step 3: Set the InParameters object's properties.
inparams.hDefKey = HKEY_LOCAL_MACHINE;
inparams.sSubKeyName = ENVIRONMENT_SUBKEY;
inparams.sValueName = "Path";
// END CALLOUT C

// CALLOUT D
// Step 4: Call ExecMethod_ to return an OutParameters object.
outparams = regprov.ExecMethod_(method.Name, inparams);
// END CALLOUT D

// CALLOUT E
// Step 5: The OutParameters object contains the method's results.
if (outparams.ReturnValue == 0) {
  systempath = outparams.sValue;
  WScript.Echo(systempath);
}
// END CALLOUT E
我偏爱纯白色 2024-11-06 16:09:48

1) 请确认我对 oReg = GetObject("WinMgmts:\.\root\default:StdRegProv"); 的用法是否正确并且与代码中给出的相同?如果没有请建议正确的用法?

在这种情况下,正斜杠 (/) 和反斜杠 (\) 都可以使用。但是,反斜杠需要加倍,因为它们是 JScript 中的特殊字符。

2) Jscirpt 中 GetExpandedStringValue 的类似函数是什么?如果没有,在获取值之前验证注册表项是否存在的更好方法是什么?

实际上,您可以在 JScript 中使用 StdRegProv.GetExpandedStringValue,即使此方法使用 out 参数并且 JScript 本身并不支持 out 参数。诀窍是通过 ExecMethod_。有关说明和示例,请参阅在 JScript 中编写 WMI 脚本

1) Pl confirm is my usage of oReg = GetObject("WinMgmts:\.\root\default:StdRegProv"); is correct and is same as the one that was given in the code? if not please suggest the right usage?

Both forward slashes (/) and backslashes (\) will work in this case. However, backslashes will need to be doubled since they are special characters in JScript.

2) what is the similar function for GetExpandedStringValue in Jscirpt? If there none, what is the better way to validate if a registry key exists before getting the value?

Actually, you can use StdRegProv.GetExpandedStringValue in JScript, even though this method uses an out parameter and JScript doesn't natively support out parameters. The trick is to call it via the ExecMethod_. See Writing WMI Scripts in JScript for an explanation and example.

白龙吟 2024-11-06 16:09:48

任何带有 \ 字符的字符串都需要转义;

var sProfileRegBase = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList" 

这包括您的 GetObject() 调用。

GetExpandedStringValue 是一样的;它不是 VB 函数,它是“Wscript.Network”对象的方法,因此可用于 js。

Any string with a \ character needs to be escaped so;

var sProfileRegBase = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList" 

This includes your GetObject() call.

GetExpandedStringValue is the same; its not a VB function, its a method of the "Wscript.Network" object so is available to js.

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