获取然后使用VBS在注册表中设置值

时间:2014-12-02 11:00:23

标签: vbscript registry

我正在尝试从注册表中检索代理覆盖列表,以便我可以将hosts文件中的本地条目添加到其中。

为此,我使用LocalClass_StdRegProv.GetStringValue(path, key),但似乎无法获得正确的值。

以下代码没有错误,但override的值只是0

我做错了什么?

然后脚本告诉我将值设置回注册表时出现错误,如果它试图将字符串值设置为整数,这有点可以预料,但是如果我做错了那么我把头抬起头来。感谢。

'** Set the HKCU as a constant *'
Const HKEY_CURRENT_USER = &H80000001

Dim LocalClass_StdRegProv
Set LocalClass_StdRegProv = GetObject("winmgmts:{impersonationlevel=impersonate}!\\.\root\default:StdRegProv")

'** Set the proxy override list *'
Dim overrideList(1)
overrideList(0) = "local.latestwordpress.com"
overrideList(1) = "local.fileshare.com"

'** Set the proxy override string to include entries from localhost *'
Dim result, override
override = LocalClass_StdRegProv.GetStringValue(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyOverride")
override = updateOverride(override, overrideList)
result = LocalClass_StdRegProv.SetStringValue(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyOverride", override)
checkResult(result)

'** Reset the objects *'
Call resetObjects

'** Quit the script *'
WScript.Quit


'**
 ' Update the proxy override list (if required) to include any local sites
 '
 ' @param required string override      The proxy override string to check
 ' @param required array overrideList   A list of addresses to include in the proxy override list
 '*
Function updateOverride(override, overrideList)

    Dim item

    '** Loop through each item in the override list... *'
    For Each item In overrideList

        '** Check to see if the current item is missing from the proxy override string... *'
        If Not InStr(override, item) Then
            override = item & ";" & override    ' Add the current to the proxy override string
        End If

    Next

    updateOverride = override

End Function


'**
 ' Check the result of a Registry key edit to ensure that it was valid
 '
 ' @param required integer result   The result of the Registry key edit to check
 '*
Function checkResult(result)

    '** Check to see if there is a VBS error or if the regestry set failed *'
    If Err.Number <> 0 Or result <> 0 Then

        '** Display an error message to the user *'
        Dim message, title
        message = "An error occured while updating your proxy settings." & vbCrLf & vbCrlF & "In order to use the internet you must manually set your proxy settings via Internet Explorer."
        title = "Error setting proxy"
        MsgBox message, vbCritical, title

        '** Reset the objects *'
        Call resetObjects

        '** Quit the script *'
        WScript.Quit

    End If

End Function


'**
 ' Reset the objects that have been used in this script
 '*
Function resetObjects()

    Set LocalClass_StdRegProv = Nothing

End Function

修改 - 添加了缺少HKEY_CURRENT_USER的声明。

1 个答案:

答案 0 :(得分:3)

override的值始终为0,因为您错误地调用了GetStringValue。这样:

override = LocalClass_StdRegProv.GetStringValue(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyOverride")

应该是这样的:

LocalClass_StdRegProv.GetStringValue HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyOverride", override

即。第4个参数是docs中所述的out参数。 GetStringValue的返回值实际上表示成功/失败

其他内容看起来没问题,如果result执行的写操作成功,则0将为SetStringValue