Skip to content

Instantly share code, notes, and snippets.

@aaronsb
Last active July 7, 2020 18:58
Show Gist options
  • Save aaronsb/46bba9f9e7b9a2e62a37e9cec240e283 to your computer and use it in GitHub Desktop.
Save aaronsb/46bba9f9e7b9a2e62a37e9cec240e283 to your computer and use it in GitHub Desktop.
Hosts management tool for linux and windows
<#
Hosts file editor: 🄯2020 Aaron Bockelie
Host Editor works on linux and windows, since my default environment is pscore on linux.
The fastest way to use this is copy-pasta the script into a ps1 file and dot source it in your session.
Get-HostEntries: Lists the host entries as objects
Remove-HostEntry: Removes a host entry by hostname or line number (use Get-HostEntries to get the line numbers)
Add-HostEntry: Appends a host entry to hosts
Notes:
Use -confirm to actually perform the action, otherwise proposed changes are just listed to screen.
Script is OS aware. It will look for hosts in /etc/hosts on a unix platform, and $windir/system32/drivers/etc/hosts on a windows platform.
On liunx, you'll need to "sudo pwsh" and dot source it from there to make changes.
#>
function Locate-HostsFile
{
switch ($PSVersionTable.Platform) {
Unix {gci "/etc/hosts"}
Win32NT {gci (Join-Path -Path $env:WINDIR -ChildPath "system32/drivers/etc/hosts")}
}
}
function Get-HostEntries
{
$hostsFile = Get-Content (Locate-HostsFile).FullName
$entries = [System.Collections.ArrayList]@()
$i=0
do {
if (!$hostsFile[$i].StartsWith("#"))
{
if ($hostsFile[$i]) {
$hostsEntry = $null
$hostsEntry = [PSCustomObject]@{
"Line"=$i;
"Name"=($hostsFile[$i] -Split "\s+")[1];
"Address"=($hostsFile[$i] -Split "\s+")[0]
}
$entries.Add($HostsEntry) | Out-Null
}
}
$i++
}
until ($i -eq $hostsFile.Length)
$entries
}
function Remove-HostEntry
{
[CmdletBinding(DefaultParameterSetName='HostName')]
param (
[int][Parameter(ParameterSetName='Line', Position=0)]$Line,
[string][Parameter(ParameterSetName='Hostname', Position=0)]$Hostname,
[switch]$confirm
)
$hostsFile = Get-Content (Locate-HostsFile).FullName
$newHostsFile = @()
$hostEntries = Get-HostEntries
if ($hostname) {
$lineItem = ($hostEntries | ?{$_.Name -eq $hostname})
if ($lineItem.Count -gt 1) {
#There's probably a better way to manage the multime named hosts corner case
#usually this is for round robin hosts lookup and should be done in DNS not in a hosts file
#it would be fairly easy to make this remove all entries matching this name, though
Throw "Ambiguous host entry (returned more than one match). Try removing by line number."
$lineItem = $lineItem[0]
}
}
if ($Line) {
$lineItem = ($hostEntries | ?{$_.Line -eq $Line})
}
$i = 0
if ($confirm) {
do {
if ($i -ne $lineItem.Line) {
$newHostsFile += $hostsFile[$i]
}
$i++
} until ($i -eq $hostsFile.Length)
$newHostsFile | Out-File (Locate-HostsFile).FullName
}
else {
Write-Warning "Hosts file not updated. Use -Confirm to perform action. Proposed changes below."
do {
if ($i -ne $lineItem.Line) {
(" " + ('{0:d3}' -f $i) + " @ " + $hostsFile[$i])
}
else {
(('{0:d3}' -f $i) + " - " + $hostsFile[$i])
}
$i++
} until ($i -eq $hostsFile.Length)
return
}
}
function Add-HostEntry
{
param (
$HostName,
$Address,
[switch]$Confirm
)
switch ($PSVersionTable.Platform) {
Unix {$Delimiter = "`t"}
Win32NT {$Delimiter = "`t`t"}
}
$hostsFile = Get-Content (Locate-HostsFile).FullName
$NewHostEntry = ($Address + $Delimiter + $Hostname)
$i = 0
if ($confirm) {
Add-Content -Path (Locate-HostsFile).FullName -Value $NewHostEntry
}
else {
Write-Warning "Hosts file not updated. Use -Confirm to perform action. Proposed changes below."
do {
(" " + ('{0:d3}' -f $i) + " @ " + $hostsFile[$i])
$i++
} until ($i -eq $hostsFile.Length)
(('{0:d3}' -f $i) + " + " + $NewHostEntry)
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment