2011-04-05 49 views
1

如何將以下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 

我部分成功地轉換,但堅持2件事。

  1. 請確認我的oReg = GetObject("WinMgmts:\\\\.\\root\\default:StdRegProv");用法是否正確,是否是相同是在代碼中給出的一個。如果不是,請提出正確的用法。

  2. JScript中的GetExpandedStringValue的等效項是什麼?如果沒有,在獲取該值之前驗證註冊表項是否存在的更好方法是什麼?

回答

1

這裏是一個樣品溶液:(來自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 
+0

非常棒..非常感謝 – svv 2011-04-05 15:19:32

+0

謝謝,你也爲我節省了很多時間! – GrandmasterB 2012-07-04 06:59:09

0

任何帶有\字符的字符串都需要被轉義;

var sProfileRegBase = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList" 

這包括您的GetObject()調用。

GetExpandedStringValue是一樣的;它不是VB函數,它是「Wscript.Network」對象的一種方法,因此可用於js。

+0

哎呀!你看到這是因爲我在這裏編輯不好。我使用GetExpandedStringValue,但它爲sProfilePath返回null,儘管它存在..我使用函數作爲oReg.GetExpandedStringValue(HKLM,sProfileRegBase +「\\」+ objEnum.SID,「ProfileImagePath」,sProfilePath); objEnum是查詢集合項的枚舉器對象oWMI.ExecQuery(「Select Name,SID from Win32_UserAccount」); – svv 2011-04-05 14:49:41

1

1)PL確認是我OREG = GetObject的的使用( 「WinMgmts:\ \根\默認:的StdRegProv」);是正確的並且與代碼中給出的一樣?如果不是,請提出正確的用法?

兩個正斜槓(/)和反斜線(\)將在這種情況下工作。然而,反斜槓需要加倍,因爲它們是JScript中的特殊字符。

2)Jscirpt中的GetExpandedStringValue類似的函數是什麼?如果沒有,在獲取值之前驗證註冊表項是否存在的更好方法是什麼?

其實,你可以在JScript中使用StdRegProv.GetExpandedStringValue,即使這種方法使用out參數和JScript本身不支持out參數。訣竅是通過ExecMethod_來致電。有關說明和示例,請參見Writing WMI Scripts in JScript

+0

感謝您的建議海倫..它的幫助。 – svv 2011-04-05 15:20:06