创建注册表项(和子项)?
Create Registry Key (and Subkeys)?
我正在尝试创建一个注册表项和子项,以便为计算机上的所有用户启用 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
除了您发布的代码示例不完整之外,您的代码还有几个问题。
"winmgmts:{impersonationLevel = impersonate}! \" & strComputer & "\root\default:StdRegProv"
WMI moniker 在安全设置和路径 (...! \...
) 之间包含虚假的 space。删除它。
附带说明一下,如果主机名从不更改,则使用主机名变量毫无意义。
strPath = strKeyPath & "\" & strSubPath
在定义构建路径的变量之前定义 strPath
。此外,您的路径组件被定义为字符串文字,因此您可以删除连接和附加变量并简单地将 strPath
定义为字符串文字。
ObjRegistry.CreateKey (HKEY_LOCAL_MACHINE, strPath)
除非在子表达式上下文中调用 function/method/procedure,否则不得将参数列表放在括号中。有关详细信息,请参阅 here。但是,您可能需要检查方法调用的 return 值以查看它们是否成功。
和 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
我正在尝试创建一个注册表项和子项,以便为计算机上的所有用户启用 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
除了您发布的代码示例不完整之外,您的代码还有几个问题。
"winmgmts:{impersonationLevel = impersonate}! \" & strComputer & "\root\default:StdRegProv"
WMI moniker 在安全设置和路径 (...! \...
) 之间包含虚假的 space。删除它。
附带说明一下,如果主机名从不更改,则使用主机名变量毫无意义。strPath = strKeyPath & "\" & strSubPath
在定义构建路径的变量之前定义strPath
。此外,您的路径组件被定义为字符串文字,因此您可以删除连接和附加变量并简单地将strPath
定义为字符串文字。ObjRegistry.CreateKey (HKEY_LOCAL_MACHINE, strPath)
除非在子表达式上下文中调用 function/method/procedure,否则不得将参数列表放在括号中。有关详细信息,请参阅 here。但是,您可能需要检查方法调用的 return 值以查看它们是否成功。
和 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