-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhu.ps1
80 lines (73 loc) · 2.89 KB
/
hu.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
function Check-Hosts {
$is_exist = Test-Path $hosts
return $is_exist
}
function Update-Hosts {
$url = "https://someonewhocares.org/hosts/hosts"
#Write-Host "Downloading update..." $url -ForegroundColor Green
Invoke-WebRequest -Uri $url -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::FireFox -OutFile "hosts.new"
$currentHash = Get-FileHash $hosts -Algorithm MD5
$newHash = Get-FileHash "hosts.new" -Algorithm MD5
if ($currentHash.Hash -ne $newHash.Hash) {
Copy-Item -Path $hosts -Destination "${hosts}_BACKUP"
Remove-Item -Path $hosts
Copy-Item -Path "hosts.new" -Destination $hosts
Remove-Item -Path "hosts.new"
}
else {
Remove-Item -Path "hosts.new"
exit 0
#$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
#$balloon.BalloonTipText = "No need to update hosts file!"
#$balloon.BalloonTipTitle = "Hosts updater"
#$balloon.Visible = $true
#$balloon.ShowBalloonTip(5000)
}
}
function Test-Admin {
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
# Main script entry point
if (Test-Admin) {
Write-Host "Running script with administrator privileges" -ForegroundColor Yellow
}
else {
Write-Host "Running script without administrator privileges" -ForegroundColor Red
exit 1
}
$hosts = "C:\Windows\system32\drivers\etc\hosts"
Add-Type -AssemblyName System.Windows.Forms
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
if (Check-Hosts) {
try {
Update-Hosts
#Write-Host "Operation completed" -ForegroundColor Magenta
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
$balloon.BalloonTipText = "Hosts file updated successfully!"
$balloon.BalloonTipTitle = "Hosts updater"
$balloon.Visible = $true
$balloon.ShowBalloonTip(5000)
#exit 0
}
catch [System.Exception] {
#Write-Host $_.Exception.Message -ForegroundColor Red
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Error
$balloon.BalloonTipText = "Error updating hosts file!"
$balloon.BalloonTipTitle = "Hosts updater"
$balloon.Visible = $true
$balloon.ShowBalloonTip(5000)
exit 1
}
}
else {
#Write-Host "Hosts file does not exist!" -ForegroundColor Red
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Error
$balloon.BalloonTipText = "Hosts file does not exist!"
$balloon.BalloonTipTitle = "Hosts updater"
$balloon.Visible = $true
$balloon.ShowBalloonTip(5000)
exit 1
}