Skip to content

Instantly share code, notes, and snippets.

@StefanoBelli
Last active April 11, 2017 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefanoBelli/77d9db2d0e12f35d613f6a4b0dc134b4 to your computer and use it in GitHub Desktop.
Save StefanoBelli/77d9db2d0e12f35d613f6a4b0dc134b4 to your computer and use it in GitHub Desktop.
Map ads to 127.0.0.1
Set-Variable -Name hosts -Value C:\Windows\System32\drivers\etc\hosts
Set-Variable -Name oldHosts -Value C:\Windows\System32\drivers\etc\hosts.old.apply_hostsadblock
$sources = "https://adaway.org/hosts.txt",
"https://hosts-file.net/ad_servers.txt",
"https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext"
echo " *** WARNING *** "
echo " ! This script can be executed to update the hosts file !"
echo " ! For that reason, this WILL OVERWRITE THE hosts file !"
echo " ! localhost mapping restored at the end !"
if(Test-Path $oldHosts) {
echo " ==> A copy of old hosts already exists, would you like to make a copy, or let it get overwritten?"
$answ = Read-Host -Prompt " Answer[ow/mkc]"
if($answ -eq "ow") {
echo " warn: overwriting copy file!"
} elseIf($answ -eq "mkc") {
echo " info: making copy of backup file"
echo " info: right here"
Copy $oldHosts old_hosts_copy
if(-not $?) {
echo " error: cannot copy file, aborting..."
exit 1
}
} else {
echo " error: non-valid answer, aborting..."
exit 1
}
}
echo " * Copying old hosts file..."
echo " * Copy location: $oldHosts"
Copy $hosts $oldHosts
if(-not $?) {
exit 1
}
Set-Variable -Name i -Value 0
foreach($source in $sources) {
echo " --> Downloading from: $source"
Invoke-WebRequest -Uri $source -OutFile "tmpHosts__$i" -UseBasicParsing >$null 2>&1
if(-not $?) {
echo "--! Cannot download from $source, ignoring"
continue
}
$i++
}
echo " -- Applying changes to $hosts..."
Get-Content tmpHosts__* | Set-Content $hosts
if(-not $?) {
echo " --! Something went wrong!"
exit 2
}
Del tmpHosts__*
if(-not $?) {
echo "--! Something went wrong!"
exit 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment