-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-mvpsHosts.ps1
80 lines (68 loc) · 3.08 KB
/
update-mvpsHosts.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
param([switch]$Elevated)
function checkAdmin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((checkAdmin) -eq $false) {
if ($elevated)
{
# could not elevate, quit
}
else {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
function Unzip {
param([string]$zipfile, [string]$outpath)
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
function BackupLocalHost {
param([string]$hostFilePath)
Copy-Item $hostFilePath "$hostFilePath.bak_$(Get-Date -format FileDate)"
}
function UpdateHost {
param([string]$hostFilePath, [string[]]$mvpsFileContents)
$hostFileContents = Get-Content -Path $hostFilePath
$count = 0;
foreach($hostLine in $hostFileContents)
{
if ($hostLine -eq "# This MVPS HOSTS file is a free download from: #") {
break
}
$count++
}
$finalHostFileContents = $hostFileContents[0..$($count-1)] + $mvpsFileContents
Set-Content -Path $hostFilePath -value $finalHostFileContents
Write-Host "Done: Local Host file updated"
}
function main() {
$basePath = "$env:windir\system32\drivers\etc"
$mvpsHostZipFilePath = "$basePath\mvps.zip"
if (Test-Path("$mvpsHostZipFilePath")) { Remove-Item $mvpsHostZipFilePath -Force }
$mvpsHostUnzipFolderPath = "$basePath\mvps"
if (Test-Path("$mvpsHostUnzipFolderPath")) { Remove-Item $mvpsHostUnzipFolderPath -Recurse -Force }
$mvpsHostUrl = "http://winhelp2002.mvps.org/hosts.zip"
Write-Host "Downloading latest mvps host file from $mvpsHostUrl"
Invoke-WebRequest $mvpsHostUrl -OutFile $mvpsHostZipFilePath
Unzip -zipfile $mvpsHostZipFilePath -outpath $mvpsHostUnzipFolderPath
Remove-Item $mvpsHostZipFilePath -Force
$mvpsFileContents = Get-Content -Path "$mvpsHostUnzipFolderPath\hosts"
$mvpsLicenseFileContents = Get-Content -Path "$mvpsHostUnzipFolderPath\license.txt"
Remove-Item $mvpsHostUnzipFolderPath -Recurse -Force
Write-Host $($mvpsLicenseFileContents -join "`r`n" | Out-String)
$message = 'License'
$question = 'MVPS Host is protected by the above license. Are you sure you want to proceed?'
$choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))
$decision = $Host.UI.PromptForChoice($message, $question, $choices, 1)
if ($decision -eq 0) {
BackupLocalHost -hostfilePath "$basePath\hosts"
UpdateHost -hostFilePath "$basePath\hosts" -mvpsFileContents $mvpsFileContents
} else {
Write-Host 'Cancelled: Local Host file has NOT been updated'
}
}
main