此 VBScript 腳本從預定義代理列表中逐一測試連通性(Ping),發現可用代理後彈出確認對話框,用戶確認即寫入註冊表啟用 IE 代理設置。
' ieproxy.vbs
' 用法:命令提示符執行 cscript ieproxy.vbs
Option Explicit
Dim strMachines, aMachines, machine2, machinearr, machine
Dim objPing, objStatus, objReg, strComputer, strKeyPath, strEntryName, dwValue
' 代理列表(IP:Port),實際使用請替換為有效代理
strMachines = "24.11.246.125:2406;61.252.60.30:68660;64.161.10.2:3128;65.23.157.55:80;66.229.103.146:5089"
aMachines = Split(strMachines, ";")
For Each machine2 In aMachines
machinearr = Split(machine2, ":")
machine = machinearr(0)
' WMI Ping 測試
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '" & machine & "'")
For Each objStatus In objPing
If IsNull(objStatus.StatusCode) Or objStatus.StatusCode <> 0 Then
WScript.Echo machine2 & " is not reachable"
Else
WScript.Echo machine2 & " is OK"
If Confirm("設定代理為 " & machine2 & "?") Then
MsgBox SetIEProxy(1, machine2)
End If
End If
Next
Next
' 確認對話框函數
Function Confirm(prompt)
Confirm = (MsgBox(prompt, vbYesNo, prompt) = vbYes)
End Function
' 修改註冊表設置 IE Proxy
Function SetIEProxy(ProxyEnable, ProxyIP)
On Error Resume Next
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objReg = GetObject("winmgmts:" & _
"{impersonationLevel=impersonate}\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\"
' 啟用/禁用代理
strEntryName = "ProxyEnable"
dwValue = ProxyEnable
objReg.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strEntryName, dwValue
' 設置代理地址
strEntryName = "ProxyServer"
dwValue = ProxyIP
objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, strEntryName, dwValue
If Err.Number = 0 Then
SetIEProxy = True
Else
SetIEProxy = False
End If
End Function
MsgBox "完成"
使用 Win32_PingStatus 類,比傳統 Shell.Run "ping" 更精準獲取狀態碼(0 表示成功)。
HKEY_CURRENT_USER (當前用戶)。Software\Microsoft\Windows\CurrentVersion\Internet Settings。ProxyEnable (DWORD, 1=啟用),ProxyServer (字符串, "ip:port")。On Error Resume Next 配合 Err.Number 判斷註冊表寫入是否成功。