-1

I am trying to create a registry key and subkey for enabling IE 11 enterprise mode for all users on a machine. This is what I am using for my VBScript currently and it is failing horribly (does not add the key). I could use some assistance in getting this corrected.

    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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
blaq
  • 59
  • 1
  • 8
  • 3
    Try sticking this line `strPath = strKeyPath & "\" & strSubPath` AFTER you set `strKeyPath` and `strSubPath`. Currently you have it before, so `strPath` is probably NULL when it's used in your `ObjRegistry.CreateKey` call. – JNevill Jan 05 '16 at 19:17
  • No this did not resolve my issue i got another error. "Cannot use parentheses when calling a Sub" – blaq Jan 05 '16 at 20:09
  • What error are you getting? Where you getting an error before? – JNevill Jan 05 '16 at 20:10
  • "Cannot use parentheses when calling a Sub" – blaq Jan 05 '16 at 20:11
  • This might help with that: http://stackoverflow.com/questions/14902134/cannot-use-parentheses-when-calling-a-sub-error-800a0414-vbs – JNevill Jan 05 '16 at 20:12
  • So after removing the () from (HKEY_LOCAL_MACHINE, strPath). I get the error "Expected Statement" 800A0400 – blaq Jan 05 '16 at 20:17

1 Answers1

1

There are several issues with your code, aside from the fact that you posted an incomplete code sample.

  • "winmgmts:{impersonationLevel = impersonate}! \\" & strComputer & "\root\default:StdRegProv"
    The WMI moniker contains a spurious space between security settings and path (...! \\...). Remove it.
    As a side note, it's pointless to use a variable for the hostname if that hostname never changes.
  • strPath = strKeyPath & "\" & strSubPath
    You define strPath before you define the variables you build the path from. Also, your path components are defined as string literals, so you could drop the concatenation and the additional variables and simply define strPath as a string literal.
  • ObjRegistry.CreateKey (HKEY_LOCAL_MACHINE, strPath)
    You must not put argument lists in parentheses unless you're calling the function/method/procedure in a subexpression context. See here for more details. However, you may want to check the return value of your method calls to see if they were successful.

And FTR, hungarian notation is pointless code bloat. Don't use it.

Modified code:

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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328