svv svv - 2 months ago 9
Javascript Question

Converting the code from vbscript to Jscript?

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.


  1. 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.

  2. 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?


Answer

Here is a sample solution: (from 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