2016-01-05 84 views
-1

我正在嘗試創建一個註冊表項和子項,以便爲機器上的所有用戶啓用IE 11企業模式。這就是我目前使用的VBScript,它失敗的可怕(不添加密鑰)。我可以使用一些幫助來糾正這個問題。創建註冊表項(和子項)?

Const HKEY_LOCAL_MACHINE = &H80000002 

    strComputer = "." 

    Set ObjRegistry = _ 
     GetObject("winmgmts:{impersonationLevel = impersonate}! \\" & _ 
     strComputer & "\root\default:StdRegProv") 

    strPath = strKeyPath & "\" & strSubPath 
    strKeyPath = "Software\Policies\Microsoft" 
    strSubPath = "Internet Explorer\Main\EnterpriseMode" 
    strName = "Enabled" 

    ObjRegistry.CreateKey (HKEY_LOCAL_MACHINE, strPath) 
    ObjRegistry.SetStringValue HKEY_LOCAL_MACHINE, strPath, strName, strValue 
    MsgBox "Successfully enabled Internet Explorer Enterprise Mode." 
End Function 
+3

嘗試在設置'strKeyPath'和'strSubPath'粘附這一行'strPath的= strKeyPath的& 「\」 &strSubPath'。目前你已經有了它,所以在你的'ObjRegistry.CreateKey'調用中使用'strPath'可能是NULL。 – JNevill

+0

沒有這沒有解決我的問題,我得到了另一個錯誤。 「調用Sub時不能使用圓括號」 – blaq

+0

你得到了什麼錯誤?你之前得到錯誤的地方? – JNevill

回答

1

除了您發佈了不完整的代碼示例外,代碼還有幾個問題。

  • "winmgmts:{impersonationLevel = impersonate}! \\" & strComputer & "\root\default:StdRegProv"
    的WMI moniker包含安全設置和路徑(...! \\...)之間的僞空間。去掉它。
    作爲一個方面說明,如果主機名永不改變,則對主機名使用變量是毫無意義的。
  • strPath = strKeyPath & "\" & strSubPath
    在定義構建路徑的變量之前,您先定義strPath。此外,您的路徑組件被定義爲字符串文字,因此您可以放棄連接和附加變量,並簡單地將strPath定義爲字符串文字。
  • ObjRegistry.CreateKey (HKEY_LOCAL_MACHINE, strPath)
    除非您在子表達式上下文中調用函數/方法/過程,否則不得在圓括號中放置參數列表。有關更多詳細信息,請參閱here。但是,您可能需要檢查方法調用的返回值以查看它們是否成功。

而FTR,hungarian notation是無意義的代碼膨脹。不要使用它。

修改後的代碼:

Function SetEnterpriseMode(value) 
    Const HKLM = &h80000002 

    Set reg = GetObject("winmgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv") 

    path = "Software\Policies\Microsoft\Internet Explorer\Main\EnterpriseMode" 
    name = "Enabled" 

    rc = reg.CreateKey(HKLM, path) 
    If rc <> 0 Then 
     MsgBox "Cannot create key (" & rc & ")." 
     Exit Function 
    End If 

    rc = reg.SetStringValue(HKLM, path, name, value) 
    If rc = 0 Then 
     MsgBox "Successfully enabled Internet Explorer Enterprise Mode." 
    Else 
     MsgBox "Cannot set value (" & rc & ")." 
    End If 
End Function 
+0

感謝您向我提供此信息。 – blaq