将代码从 vbscript 转换为 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
我在转变方面取得了部分成功,但坚持了两件事。
请确认我的使用方式是否正确并且与给出的相同在代码中。如果不是,请建议正确的用法。
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.
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.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个示例解决方案:(来自 http://www.windowsitpro.com/content /content/93402/Listing_05.txt)
Here is a sample solution: (from http://www.windowsitpro.com/content/content/93402/Listing_05.txt)
在这种情况下,正斜杠 (
/
) 和反斜杠 (\
) 都可以使用。但是,反斜杠需要加倍,因为它们是 JScript 中的特殊字符。实际上,您可以在 JScript 中使用
StdRegProv.GetExpandedStringValue
,即使此方法使用out
参数并且 JScript 本身并不支持out
参数。诀窍是通过ExecMethod_
。有关说明和示例,请参阅在 JScript 中编写 WMI 脚本。Both forward slashes (
/
) and backslashes (\
) will work in this case. However, backslashes will need to be doubled since they are special characters in JScript.Actually, you can use
StdRegProv.GetExpandedStringValue
in JScript, even though this method uses anout
parameter and JScript doesn't natively supportout
parameters. The trick is to call it via theExecMethod_
. See Writing WMI Scripts in JScript for an explanation and example.任何带有 \ 字符的字符串都需要转义;
这包括您的
GetObject()
调用。GetExpandedStringValue
是一样的;它不是 VB 函数,它是“Wscript.Network”对象的方法,因此可用于 js。Any string with a \ character needs to be escaped so;
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.