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

Windows自动方式可以不使用git bash #274

Open
gxlydlyf opened this issue Feb 22, 2025 · 1 comment
Open

Windows自动方式可以不使用git bash #274

gxlydlyf opened this issue Feb 22, 2025 · 1 comment

Comments

@gxlydlyf
Copy link

首先创建fetch_github_hosts.bat文件并写入以下内容

@echo off

powershell -Command "& { $remote='https://raw.hellogithub.com/hosts'; $hostsPath='%SystemRoot%\System32\drivers\etc\hosts'; $reader=[System.IO.StreamReader]::new($hostsPath, $true); $hostsContent=$reader.ReadToEnd(); $encoding=$reader.CurrentEncoding; $reader.Close(); $newContent=$encoding.GetString((Invoke-WebRequest -Uri $remote -UseBasicParsing).Content).TrimEnd(); $pattern='(?s)# GitHub520 Host Start.*?# Github520 Host End'; $newHostsContent=$hostsContent -replace $pattern, ''; if (-not $newHostsContent.EndsWith([Environment]::NewLine)) { $newHostsContent+=[Environment]::NewLine; } [System.IO.File]::WriteAllText($hostsPath, $newHostsContent + $newContent, $encoding); }"

echo GitHub hosts updated successfully!

然后使用使用管理员身份运行以下cmd命令添加到task schtasks,会在每天05:00运行fetch_github_hosts.bat文件,记得把path\to\fetch_github_hosts.bat修改为你的文件所在路径

schtasks /create /tn FetchGithubHosts /tr "path\to\fetch_github_hosts.bat" /sc DAILY /st 05:00 /ru SYSTEM /rp "" /rl HIGHEST /f

然后如果要手动运行,使用管理员身份运行以下cmd命令

schtasks /run /tn FetchGithubHosts

如果要删除task schtasks,使用管理员身份运行以下cmd命令

schtasks /delete /tn FetchGithubHosts /f
@woaiwuhengyue
Copy link

woaiwuhengyue commented Feb 22, 2025 via email

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

2 participants