-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-mvpsHosts.ps1
58 lines (46 loc) · 2.28 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
#Requires -RunAsAdministrator
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip {
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
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"
}
$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:https://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) {
UpdateHost -hostFilePath "$basePath\hosts" -mvpsFileContents $mvpsFileContents
} else {
Write-Host 'Cancelled: Local Host file has NOT been updated'
}