Skip to content

Instantly share code, notes, and snippets.

@matijagrcic
Last active May 30, 2016 06:08
Show Gist options
  • Save matijagrcic/17d6c32d3794b667a9a1 to your computer and use it in GitHub Desktop.
Save matijagrcic/17d6c32d3794b667a9a1 to your computer and use it in GitHub Desktop.
$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