自己喜欢折腾 hosts 文件,比如下面这是 Postman7 系列最终版屏蔽更新用的.
目前已经可以通过 PowerShell 可以正常维护 hosts 文件
虽然 PowerShell 更方便语法也更简洁,但是需要额外执行一次 set-ExecutionPolicy RemoteSigned 才可以运行脚本感觉还是有点麻烦,所以还是折腾下 bat 脚本文件, 但是输出有问题但不知道出在哪
@echo off
setlocal enabledelayedexpansion
chcp 65001 > nul
:: 提权到管理员权限
net session >nul 2>&1
if %errorlevel% neq 0 (
echo Requesting administrative privileges...
powershell -Command "Start-Process '%~0' -Verb runAs"
exit /B
)
:: 定义要添加的规则列表
set "rules[0]=127.0.0.1 dl.pstmn.io"
set "rules[1]=127.0.0.1 onboarding.postman.com"
set "rules[2]=127.0.0.1 analytics.getpostman.com"
set "rules[3]=127.0.0.1 assets.getpostman.com"
set "rules[4]=127.0.0.1 static.getpostman.com"
set "rules[5]=127.0.0.1 skills.gw.postman.com"
set "rules[6]=127.0.0.1 skills-assets.pstmn.io"
set "rules[7]=127.0.0.1 skills.getpostman.com"
set "rules[8]=127.0.0.1 config.getpostman.com"
set "rules[9]=127.0.0.1 cdn.postman.com"
set "rules[10]=127.0.0.1 getpostman.com"
set "rules[11]=127.0.0.1 www.postman.com"
set "rules[12]=#127.0.0.1 identity.getpostman.com"
set "rules[13]=#127.0.0.1 sync-v3.getpostman.com"
:: 设置 hosts 文件路径
set "hostsFile=%windir%\System32\drivers\etc\hosts"
:: 备份原始 hosts 文件
copy "%hostsFile%" "%hostsFile%.bak" >nul
:: 移除 hosts 文件的只读属性
attrib -R "%hostsFile%"
:: 读取每条规则并进行处理
for %%a in (0,1,13) do (
set "line=!rules[%%a]!"
echo Current: !line!
:: 检查该规则是否已存在于 hosts 文件中
findstr /C:"!line!" "%hostsFile%" >nul
if errorlevel 1 (
:: 如果未找到,则添加到 hosts 文件
echo !line! >> "%hostsFile%"
echo Added: !line!
) else (
echo Skipped: !line! already exists
)
)
:: 重新添加 hosts 文件的只读属性
attrib +R "%hostsFile%"
echo Done.
endlocal
pause
不知道为啥一共 13 条记录输出界面只打印下面 3 条,相应的 hosts 文件里也只出现了这三条, 百思不得其解, 求指教
Current: 127.0.0.1 dl.pstmn.io
Skipped: 127.0.0.1 dl.pstmn.io already exists
Current: 127.0.0.1 onboarding.postman.com
Skipped: 127.0.0.1 onboarding.postman.com already exists
Current: #127.0.0.1 sync-v3.getpostman.com
Skipped: #127.0.0.1 sync-v3.getpostman.com already exists
Done.
Press any key to continue . . .
|