gpt4 book ai didi

javascript - 将代码从 vbscript 转换为 Jscript?

转载 作者:行者123 更新时间:2023-11-30 18:45:48 25 4
gpt4 key购买 nike

如何将以下 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. JScript 中 GetExpandedStringValue 的等价物是什么?如果没有,在获取值之前验证注册表项是否存在的更好方法是什么?

最佳答案

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

关于javascript - 将代码从 vbscript 转换为 Jscript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5553470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com