-
-
Save bluPhy/e13b2b6c25d5dc2c67e0c59855ce991c to your computer and use it in GitHub Desktop.
Powershell script for adding/removing/viewing entries to the hosts file.
This file contains hidden or 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
# | |
# Powershell Functions for adding/removing/showing entries to the hosts file. | |
# | |
# Known limitations: | |
# - does not handle entries with comments afterwards ("<ip> <host> # comment") | |
# | |
Function Use-RunAs { | |
# Check if script is running as Administrator and if not use RunAs | |
# Use Check Switch to check if admin | |
param([Switch]$Check) | |
# Detecting OS Platform, win32nt for Windows or unix for Linux/Unix | |
$OSPlatform = (([System.Environment]::OSVersion.Platform).ToString()).ToLower() | |
if ($OSPlatform -eq "unix") { | |
Write-Warning "Detected Linux/Unix OS, you don't need this, use sudo" | |
exit # Quit this session of powershell | |
} | |
$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()` | |
).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") | |
if ($Check) { return $IsAdmin } | |
if ($MyInvocation.ScriptName -ne "") { | |
if (-not $IsAdmin) { | |
try { | |
# Detecting Powershell (powershell.exe) or Powershell Core (pwsh), will return true if Powershell Core (pwsh) | |
if ($IsCoreCLR) { $PowerShellCmdLine = "pwsh.exe" } else { $PowerShellCmdLine = "powershell.exe" } | |
$arg = "-file `"$($MyInvocation.ScriptName)`"" | |
Start-Process "$PSHOME\$PowerShellCmdLine" -Verb Runas -ArgumentList $arg -ErrorAction 'stop' | |
} | |
catch { | |
Write-Warning "Error - Failed to restart script with runas" | |
break | |
} | |
exit # Quit this session of powershell | |
} | |
} | |
else { | |
Write-Warning "Error - Script must be saved as a .ps1 file first" | |
break | |
} | |
} | |
Function Add-Host { | |
<# | |
.SYNOPSIS | |
Powershell function for adding entries to the hosts file. | |
.PARAMETER HostsFile | |
The hosts file to modify | |
.PARAMETER IP | |
The IP address for the hosts entry | |
.PARAMETER Hostname | |
The hostname for the entry | |
.EXAMPLE | |
Add-Host -HostsFile "C:\Windows\System32\drivers\etc\hosts" -IP "127.0.0.1" -Hostname "myserverhost" | |
#> | |
[cmdletbinding()] | |
param( | |
[Parameter(Mandatory = $true)][string]$HostsFile, | |
[Parameter(Mandatory = $true)][string]$IP, | |
[Parameter(Mandatory = $true)][string]$Hostname | |
) | |
Remove-Host -HostsFile $HostsFile -Hostname $Hostname | |
$IP + "`t`t" + $Hostname | Out-File -encoding ASCII -append $HostsFile | |
} | |
Function Remove-Host { | |
<# | |
.SYNOPSIS | |
Powershell function for removing entries from a hosts file. | |
.DESCRIPTION | |
Known limitations: does not handle entries with comments afterwards ("<ip> <host> # comment") | |
.PARAMETER HostsFile | |
The hosts file to modify | |
.PARAMETER Hostname | |
The hostname for the entry | |
.EXAMPLE | |
Remove-Host -HostsFile "C:\Windows\System32\drivers\etc\hosts" -Hostname "myserverhost" | |
#> | |
[cmdletbinding()] | |
param( | |
[Parameter(Mandatory = $true)][string]$HostsFile, | |
[Parameter(Mandatory = $true)][string]$Hostname | |
) | |
$c = Get-Content $HostsFile | |
$newLines = @() | |
foreach ($line in $c) { | |
$bits = [regex]::Split($line, '\t+') | |
if ($bits.count -eq 2) { | |
if ($bits[1] -ne $Hostname) { | |
$newLines += $line | |
} | |
} | |
else { | |
$newLines += $line | |
} | |
} | |
# Write file | |
Clear-Content $HostsFile | |
foreach ($line in $newLines) { | |
$line | Out-File -encoding ASCII -append $HostsFile | |
} | |
} | |
Function Show-Hosts { | |
<# | |
.SYNOPSIS | |
Powershell function for showing entries in a hosts file. | |
.DESCRIPTION | |
Known limitations: does not handle entries with comments afterwards ("<ip> <host> # comment") | |
.PARAMETER HostsFile | |
The hosts file to access | |
.EXAMPLE | |
Show-Hosts -HostsFile "C:\Windows\System32\drivers\etc\hosts" | |
#> | |
[cmdletbinding()] | |
param( | |
[Parameter(Mandatory = $true)][string]$HostsFile | |
) | |
$c = Get-Content $HostsFile | |
foreach ($line in $c) { | |
$bits = [regex]::Split($line, '\t+') | |
if ($bits.count -eq 2) { | |
Write-Host $bits[0] `t`t $bits[1] | |
} | |
} | |
} | |
# | |
# Example Usage | |
# | |
Use-RunAs | |
"Script Running As Administrator" | |
Start-Sleep 3 | |
$file = "$env:SystemRoot" + "\System32\drivers\etc\hosts" | |
try { | |
if ($args[0] -eq "add") { | |
if ($args.count -lt 3) { | |
throw "Not enough arguments for add." | |
} | |
else { | |
Add-Host -HostsFile $file -IP $args[1] -Hostname $args[2] | |
} | |
} | |
elseif ($args[0] -eq "remove") { | |
if ($args.count -lt 2) { | |
throw "Not enough arguments for remove." | |
} | |
else { | |
Remove-Host -HostsFile $file -Hostname $args[1] | |
} | |
} | |
elseif ($args[0] -eq "show") { | |
Show-Hosts -HostsFile $file | |
} | |
else { | |
throw "Invalid operation '" + $args[0] + "' - must be one of 'add', 'remove', 'show'." | |
} | |
} | |
catch { | |
Write-Host $error[0] | |
Write-Host "`nUsage: hosts add <ip> <hostname>`n hosts remove <hostname>`n hosts show" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment