Converting the code from vbscript to Jscript?
如何将以下VBScript代码转换为JScript,该代码用于获取所有用户的用户配置文件路径?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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 Wscript.Echo"Profile path:" & sProfilePath Wscript.Echo ' blank line Next |
我在转换方面取得了部分成功,但遇到了两件事。
请确认我的
oot\\\\default:StdRegProv");
JScript中
1) Pl confirm is my usage of oReg = GetObject("WinMgmts:\\.\
oot\\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 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?
实际上,您可以在JScript中使用
这是一个示例解决方案:(来自http://www.windowsitpro.com/content/content/93402/Listing_05.txt)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | // 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 | var sProfileRegBase ="SOFTWARE\\\\Microsoft\\\\Windows NT\\\\CurrentVersion\\\\ProfileList" |
这包括您的