Skip to content

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

@willasas

Description

@willasas
  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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions