将代码从vbscript转换为Jscript
Converting the code from vbscript to Jscript?
如何将以下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
我部分成功地转化了,但却停留在两件事上。
请确认我对
oReg = GetObject("WinMgmts:''''.''root''default:StdRegProv");
的使用是否正确,是否与代码中给出的相同。什么是等效的
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的一个方法。
- 如何在JavaScript中将字符串转换为函数引用
- 如何在Javascript中将JSon对象转换为数组
- 使用JS将数组转换为json对象
- 如何使用json将对象列表从java转换为javascript
- 偶尔结结巴巴地说“;堆叠的”;translate()上的转换(v4.0.0-alpha40)
- 为什么在单独的函数中应用时转换会闪烁/断断续续(D3)
- 在Javascript中转换对象数组
- 将数字转换为一定数量的硬币
- 将纯文本URL转换为可单击链接
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 如何使用js将SNAPSHOT内部版本号转换为3位数的整数
- 如何将TypeScript对象转换为普通对象
- 检测个位数整数时正在转换毫秒
- 从javascript到jquery的转换
- DOM事件通过JSON转换为java
- 将圆柱体转换为弯管
- 混合 ui-sref 和 $state.go 在 Angular ui-router 中进行状态转换
- 将VBScript代码转换为javascript(即JScript)代码
- 需要帮助将vbs代码转换为jscript.我得到一个运行时错误类型不匹配
- 将代码从vbscript转换为Jscript