You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell自动检测代理设置变更并重置为空?

解决方案:自动检测并重置代理设置

一、PowerShell检测并重置代理的脚本

以下脚本会读取当前用户的系统代理配置,对比预设的空值,一旦发现代理被修改就自动重置:

# 定义预期的空代理配置
$expectedProxy = ""
$expectedProxyOverride = "<local>"

# 获取当前系统代理设置
$currentProxy = Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyServer -ErrorAction SilentlyContinue
$currentProxyOverride = Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyOverride -ErrorAction SilentlyContinue

# 检查代理是否被篡改
if (($currentProxy.ProxyServer -ne $expectedProxy) -or ($currentProxyOverride.ProxyOverride -ne $expectedProxyOverride)) {
    # 重置代理为空
    Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyServer -Value $expectedProxy -ErrorAction SilentlyContinue
    Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyOverride -Value $expectedProxyOverride -ErrorAction SilentlyContinue
    # 彻底关闭代理开关
    Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyEnable -Value 0 -ErrorAction SilentlyContinue
    
    # 可选:记录重置日志到本地文件
    Add-Content -Path "C:\ProxyResetLog.txt" -Value "$(Get-Date): 代理已被重置为空"
}
  • 脚本读取的是当前用户(HKCU分支)的代理注册表项,普通用户权限即可修改
  • 日志功能可帮你追踪代理被篡改的时间点,不需要可以直接删除对应行

二、设置自动运行的两种方式

1. 用Windows任务计划定时检测

  • 打开「任务计划程序」,新建任务
  • 触发器设为「按时间间隔重复」,比如每5分钟执行一次
  • 操作选择「启动程序」,程序/脚本填powershell.exe,参数填-ExecutionPolicy Bypass -File "C:\你的脚本路径\ResetProxy.ps1"
  • 按需勾选「不管用户是否登录都要运行」,覆盖多用户场景

2. 后台静默循环检测

如果需要更实时的监控,用循环脚本后台运行:

while ($true) {
    # 重复检测与重置逻辑
    $expectedProxy = ""
    $expectedProxyOverride = "<local>"
    $currentProxy = Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyServer -ErrorAction SilentlyContinue
    $currentProxyOverride = Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyOverride -ErrorAction SilentlyContinue

    if (($currentProxy.ProxyServer -ne $expectedProxy) -or ($currentProxyOverride.ProxyOverride -ne $expectedProxyOverride)) {
        Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyServer -Value $expectedProxy -ErrorAction SilentlyContinue
        Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyOverride -Value $expectedProxyOverride -ErrorAction SilentlyContinue
        Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyEnable -Value 0 -ErrorAction SilentlyContinue
        Add-Content -Path "C:\ProxyResetLog.txt" -Value "$(Get-Date): 代理已被重置为空"
    }
    # 每30秒检测一次,可自行调整间隔
    Start-Sleep -Seconds 30
}
  • 把脚本保存为ProxyMonitor.ps1,创建快捷方式,目标填powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File "C:\你的脚本路径\ProxyMonitor.ps1"
  • 将快捷方式放到「启动」文件夹,开机后会静默后台运行

注意事项

  • 如果是系统级代理(HKLM分支),需把脚本中的注册表路径改成HKLM:\Software\Microsoft\Windows\CurrentVersion\Internet Settings,但修改HKLM需要管理员权限
  • 可根据实际需求调整检测间隔,任务计划适合低频率检测,后台循环适合高频率实时监控

内容的提问来源于stack exchange,提问作者Austin98

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 04:35:38