-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathGithubHost.ps1
108 lines (78 loc) · 3.49 KB
/
GithubHost.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 声明全局变量
# 定义方法开始时输出的前景色
$LogForegroundColorStart = "Green"
# 定义方法结束时输出的前景色
$LogForegroundColorEnd = "Cyan"
# 定义方法输出警告时的前景色
$LogForegroundColorWarning = "Yellow"
$StartFlag = "# Generated by Powershell Start"
$EndFlag = "# Generated by Powershell End"
# $HostUrls01 = "https://gitlab.com/ineo6/hosts/-/raw/master/next-hosts"
$HostUrls02 = "https://raw.hellogithub.com/hosts"
$HostFilePath = "$env:windir\system32\drivers\etc\hosts"
Write-Host "Let's Start!" -Foreground $LogForegroundColorStart
# Requires -RunAsAdministrator
function Test-Admin
{
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if((Test-Admin) -eq $false)
{
#提升权限
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -ExecutionPolicy Bypass -file "{0}"' -f $PSCommandPath)
wmic process | Out-File -Encoding "Default" Z:\GithubHost.ps1 #输出为ANSI编码
Exit
}
Function Edit-Hosts
{
try
{
# $urls01 = Invoke-WebRequest $HostUrls01 | Select-Object -Expand Content
$urls02 = Invoke-RestMethod $HostUrls02
# $urls = $StartFlag + $([Environment]::NewLine) + $urls01 + $([Environment]::NewLine) + $urls02 + $EndFlag
$urls = $StartFlag + $([Environment]::NewLine) + $urls02 + $EndFlag
[System.Collections.ArrayList]$hostFile = Get-Content $HostFilePath
$startlineNum= ($hostFile | Select-String $StartFlag).LineNumber
$endlineNum= ($hostFile | Select-String $EndFlag).LineNumber
#打印对象
#Write-Host ($hostFile | Format-Table | Out-String)
#Write-Host ($urls01 | Format-Table | Out-String)
#Write-Host ($urls02 | Format-List | Out-String)
Write-Host ($urls | Format-List | Out-String)
#Write-Host "hostFile Count:" $hostFile.Count -Foreground $LogForegroundColorWarning
#Write-Host "startlineNum:" $startlineNum -Foreground $LogForegroundColorWarning
#Write-Host "endlineNum:" $endlineNum -Foreground $LogForegroundColorWarning
if ( $startlineNum -GT 0)
{
$hostFile.RemoveRange($startlineNum - 1, $endlineNum - $startlineNum + 1)
}
#may File Dead Lock
#$([Environment]::NewLine) + $urls | Add-Content $HostFilePath -Force
foreach ($url in $urls) { $hostFile += $url }
$hostFile > $HostFilePath
}
catch
{
Write-Host "`Edit-Hosts Failed, Exception:" $Error[0] -Foreground $LogForegroundColorWarning
Return $false
}
Return $true
}
if ((Edit-Hosts) -eq $false)
{
Write-Host "`nEdit-Hosts Failed" -Foreground $LogForegroundColorWarning
#Write-Host "`n请联系作者" -Foreground $LogForegroundColorWarning
"`nAny key to exit."
[Console]::Readkey() | Out-Null
Exit
}
else
{
Clear-DnsClientCache
$wshell = New-Object -ComObject Wscript.Shell
# $wshell.Popup(" 无法进入Github时, 请多次刷新",0,"温馨提示",0 + 64) 显示中文 请保存为ANSI编码
$wshell.Popup("Please refresh web while not working",0,"Tips",0 + 64)
Start-Process https://github.com/ping11700/GitHubHostScript
#Start-Process https://github.com/ping11700/LOLKit
}