-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateHostFile.ps1
116 lines (97 loc) · 3.15 KB
/
updateHostFile.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
109
110
111
112
113
114
115
116
#
# Powershell script for adding/removing/showing entries to the hosts file.
# arguments are:
# add <filename> - adds entries from the file to the hosts file
# remove <filename> - removes entries from the file from the hosts file
# show - shows the contents of the hosts file
#
# <filename> format is:
# <ip> <hostname>
#
# example:
# 127.0.0.1 youtube.com
# 127.0.0.1 twitter.com
function add-hosts([string]$filename, [array]$hostFileEntries) {
foreach($item in $hostFileEntries){
$ip = $item["ip"]
$hostname = $item["hostname"]
$ip + "`t" + $hostname | Out-File -encoding ASCII -append $filename
}
}
function remove-hosts([string]$filename, [array]$hostFileEntries) {
$c = Get-Content $filename
$newLines = @()
foreach ($line in $c)
{
[bool]$isIpAndHostnameInFile = 0
foreach($item in $hostFileEntries)
{
$ip = $item["ip"]
$hostname = $item["hostname"]
$bits = [regex]::Split($line, "\s+|\t+")
if ($bits[0] -eq $ip -and $bits[1] -eq $hostname) {
$isIpAndHostnameInFile = 1
break
}
}
if ($isIpAndHostnameInFile -eq 0)
{
$newLines += $line
}
}
# Write file
Clear-Content $filename
foreach ($line in $newLines) {
$line | Out-File -encoding ASCII -append $filename
}
}
function print-hosts([string]$filename) {
$c = Get-Content $filename
foreach ($line in $c) {
Write-Host $line
}
}
function get-hosts([string]$filename) {
$c = Get-Content $filename
$hostFileEntries = @()
foreach ($line in $c) {
$bits = [regex]::Split($line, "\s+|\t+")
$hostFileEntries += @{
"ip" = $bits[0];
"hostname" = $bits[1]
}
}
return $hostFileEntries
}
try {
$hostFile = "C:\Windows\System32\drivers\etc\hosts"
$logFile = "C:\temp\updateHostFile.log"
# Log arguments and date
"$args $(Get-Date -Format "MM/dd/yyyy HH:mm K") "| Out-File $logFile -Append
# Requires -RunAsAdministrator
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
throw "Script must be run as administrator."
}
if ($args[0] -eq "show") {
print-hosts $hostFile
} elseif ($args[0] -eq "add" -or $args[0] -eq "remove") {
# Flush DNS cache
ipconfig /flushdns
$hostFileEntries = get-hosts $args[1]
if ($args[0] -eq "add") {
remove-hosts $hostFile $hostFileEntries
add-hosts $hostFile $hostFileEntries
} elseif ($args[0] -eq "remove") {
remove-hosts $hostFile $hostFileEntries
}
print-hosts $hostFile
} else {
throw "Invalid operation '" + $args[0] + "' - must be one of 'add <filename>', 'remove <filename>', 'show'."
}
} catch {
$error[0] | Out-File $logFile -Append
Write-Host $error[0]
}