文章目录
- abstract
- powershell 版本的一键更新hosts文件
- 更新Hosts
- 操作步骤
- 准备:设置powershell执行策略
- powrshell脚本
- 注册计划任务定期自动执行上述操作
- 相关目录结构
- 其他方法获取相关脚本
- 一键运行整合脚本🤖🐽
- 检查
abstract
- 尽管这里实现了一键部署自动更新hosts的脚本,但是可用性仍然取决于原项目提供的hosts是否可用
- 主要用来加速访问github网站,用来直接查看原github项目链接的,对于资源下载效果不大,这方面的需要可以用加速镜像来加速下载或克隆,或者cgit等工具
powershell 版本的一键更新hosts文件
更新Hosts
这部分对应于git-bash脚本
操作步骤
- 如果你不想了解细节,直接跳转到后面的一键运行整合脚本一节
准备:设置powershell执行策略
结束之后,如果不放心,可以将bypass
重新设置为default
详情: Set-ExecutionPolicy (Microsoft.PowerShell.Security) - PowerShell | Microsoft Learn
powrshell脚本
您可以将上述脚本复制粘贴到一个文本文件中(.txt),然后保存修改,并将文件重命名为fetch-github-hosts.ps1
建议单独创建一个文件夹来存放该文件,比如C:\GithubHostUpdate
,或简单点的C:\GHU
后续计划任务会定期执行这里的脚本文件
你也可以考虑创建这个文件的快捷方式,指定不同的powershell版本(比如powershell7)来执行.ps1
,而不是系统帮你选择
注册计划任务定期自动执行上述操作
下面的代码可以作为一次性的代码,可以保存到powershell配置文件或某个模块中
如同上一步的手法,将这一段代码保存到脚本文件C:\GHU\Autofetch.ps1
可以用管理员方式运行它,或者在管理powershell中执行:
相关目录结构
其他方法获取相关脚本
从 PS/Deploy/GithubHostsUpdater · xuchaoxin1375/scripts - 码云 - 开源中国 (gitee.com)下载两个文件
gitee没有提供下载按钮,可以点击查看原始数据(或者直接点击下面的链接,然后分别保存,保存的时候后缀改为.ps1
,而不是.txt
)
- gitee.com/xuchaoxin1375/scripts/raw/main/PS/Deploy/GithubHostsUpdater/AutoFetch.ps1
- gitee.com/xuchaoxin1375/scripts/raw/main/PS/Deploy/GithubHostsUpdater/fetch-github-hosts.ps1
或者从本仓库的GHU目录下载
一键运行整合脚本🤖🐽
上面的步骤也许不是你关心的,那么一键运行了解以下
下面的代码自动下载需要的脚本文件,自动执行并注册到计划任务
管理员方式打开powershell,然后复制粘贴上述代码,运行即可
检查
- 你可以通过以下命令来立即触发更新host的任务,看看是否生效
- 例如