Create Registry Key (and Subkeys)?

后端 未结 1 1565
野趣味
野趣味 2021-01-29 08:04

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 failin

相关标签:
1条回答
  • 2021-01-29 09:01

    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
    
    0 讨论(0)
提交回复
热议问题