Last active
May 30, 2016 06:08
-
-
Save matijagrcic/17d6c32d3794b667a9a1 to your computer and use it in GitHub Desktop.
Powershell script to download from https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$hostsRequest = (Invoke-Webrequest -Uri "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts") | |
$currentDate = ((get-date).ToUniversalTime()).ToString("dd-MM-yyyy hh-mm-ss") | |
$filePath = "C:\Windows\System32\drivers\etc\host-{0}.{1}" | |
if($hostsRequest.StatusCode -eq 200){ | |
$captureDomainRegex = "^(?:\d{1}\.\d{1}\.\d{1}.\d{1} )(.+)" | |
$hosts = [PsCustomObject]@{unique=@(); duplicate=@();} | |
$hostsRequest.Content | out-file -filepath ($filePath -f $currentDate, "txt") | |
foreach ($line in Get-Content ($filePath -f $currentDate, "txt")){ | |
if($line -match $captureDomainRegex) { | |
if($hosts.unique -contains $matches[1]){ | |
$hosts.duplicate += $matches[1] | |
}else{ | |
$hosts.unique += $matches[1] | |
} | |
} | |
} | |
ConvertTo-Json $hosts | out-file -filepath ($filePath -f $currentDate, "json") | |
} | |
#load json and get the count of unique entries | |
#(Get-Content ($filePath -f $currentDate, "json") | ConvertFrom-Json).unique.Count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment