将代码从vbscript转换为Jscript

Converting the code from vbscript to Jscript?

本文关键字:Jscript 转换 vbscript 代码      更新时间:2023-09-26

如何将以下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. 请确认我对oReg = GetObject("WinMgmts:''''.''root''default:StdRegProv");的使用是否正确,是否与代码中给出的相同。

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

下面是一个示例解决方案:(来自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

1)请确认我使用oReg = GetObject("WinMgmts:'.'root'default:StdRegProv");是否正确,是否与代码中给出的相同?如果不是,请建议正确的用法?

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

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

实际上,您可以在JScript中使用StdRegProv.GetExpandedStringValue,尽管该方法使用out参数,而JScript本身不支持out参数。诀窍是通过ExecMethod_调用它。

任何带有'字符的字符串都需要转义,以便;

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

这包括你的GetObject()呼叫。

GetExpandedStringValue相同;它不是一个VB函数,而是Wscript的一个方法。