proxy/scripts/set-windows-proxy.ps1
2026-06-30 23:00:35 +08:00

37 lines
1.2 KiB
PowerShell

param(
[switch]$Enable,
[switch]$Disable,
[string]$ProxyServer = '127.0.0.1:8888',
[string]$Override = '<local>'
)
$ErrorActionPreference = 'Stop'
$key = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'
if ($Enable -eq $Disable) {
throw 'Pass exactly one of -Enable or -Disable.'
}
if ($Enable) {
Set-ItemProperty -Path $key -Name ProxyEnable -Type DWord -Value 1
Set-ItemProperty -Path $key -Name ProxyServer -Type String -Value $ProxyServer
Set-ItemProperty -Path $key -Name ProxyOverride -Type String -Value $Override
Write-Host "Enabled Windows proxy: $ProxyServer"
} else {
Set-ItemProperty -Path $key -Name ProxyEnable -Type DWord -Value 0
Write-Host 'Disabled Windows proxy.'
}
$source = @"
using System;
using System.Runtime.InteropServices;
public static class WinInetNative {
[DllImport("wininet.dll", SetLastError = true)]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
}
"@
Add-Type -TypeDefinition $source -ErrorAction SilentlyContinue | Out-Null
[WinInetNative]::InternetSetOption([IntPtr]::Zero, 39, [IntPtr]::Zero, 0) | Out-Null
[WinInetNative]::InternetSetOption([IntPtr]::Zero, 37, [IntPtr]::Zero, 0) | Out-Null