Skip to content

Instantly share code, notes, and snippets.

@johnnyhalife
Created April 4, 2010 22:15
Show Gist options
  • Save johnnyhalife/355746 to your computer and use it in GitHub Desktop.
Save johnnyhalife/355746 to your computer and use it in GitHub Desktop.
# Adds a new host entry on the hosts file as shown:
#
# add-host www.southworks.net 127.0.0.1 [optional $true|$false]
#
# the third parameter indicates whether if exists it should be overwritten
function add-host {
# map parameters
$hostname, $address, $override = $args[0], $args[1], ($args[2] -eq $true)
# generte some basics
$hosts, $entry = (gc hosts), "$hostname $address"
if([regex]::match($hosts, "\s*$hostname\s*").success){
# it already existed but chose not override
if(-not $override) { return; }
clear-content hosts
$hosts | where-object{[regex]::match($_, "\s*$hostname\s.*$").success -eq $false} | add-content hosts
}
# now append it
add-content hosts $entry
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment