Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增window系统批处理脚本 #263

Open
willasas opened this issue Dec 11, 2024 · 3 comments
Open

新增window系统批处理脚本 #263

willasas opened this issue Dec 11, 2024 · 3 comments

Comments

@willasas
Copy link

  1. 将下面代码保存为sethost.bat文件;
@echo off
setlocal

:: 设置 PowerShell 脚本编码为 UTF-8
set "PSModulePath=%PSModulePath%;%SystemRoot%\System32\WindowsPowerShell\v1.0\Modules"

:: 下载 hosts 文件并指定编码为 UTF-8
powershell -Command "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12; $webClient = New-Object System.Net.WebClient; $webClient.Encoding = [System.Text.Encoding]::UTF8; $webClient.DownloadFile('https://raw.hellogithub.com/hosts', '%temp%\hosts.txt')"

:: 检查是否下载成功
if not exist "%temp%\hosts.txt" (
    echo 下载 hosts 文件失败。
    pause
    exit /b 1
)

:: 使用 PowerShell 将内容追加到 C:\Windows\System32\drivers\etc\hosts 并指定编码为 UTF-8
powershell -Command "$hostsContent = Get-Content -Path '%temp%\hosts.txt' -Encoding UTF8; Add-Content -Path 'C:\Windows\System32\drivers\etc\hosts' -Value $hostsContent -Encoding UTF8"

:: 刷新 DNS 缓存
ipconfig /flushdns

:: 删除临时文件
del "%temp%\hosts.txt"

:: 等待3秒后关闭命令行窗口
timeout /t 3 /nobreak >nul

endlocal
  1. 以管理员身份运行上面的bat脚本即可;
  2. 注意,host中的文件地址用的是这个https://raw.hellogithub.com/hosts
@Marcus737
Copy link

会把前一次执行的内容删除后再添加新的吗

@Jackson888888
Copy link

这个是定时自动更新,还是需要手动更新?

@Ded-calm
Copy link

会把前一次执行的内容删除后再添加新的吗

实测不会,无限追加

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants