Skip to content

Commit

Permalink
Add a powershell script with batch helper to add a new host to the ho…
Browse files Browse the repository at this point in the history
…sts file.
  • Loading branch information
patrickcarlohickman committed Feb 15, 2023
1 parent 90e5cd0 commit 39c36dc
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
41 changes: 41 additions & 0 deletions bin/add-host.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@ECHO off

REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM :: Some nice startup stuff
REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SETLOCAL ENABLEEXTENSIONS
SET me=%~n0
SET parent=%~dp0

REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM :: Define the exit codes for the script. Powers of 2 for bitwise work.
REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SET /A errno=0

REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM :: Define main variables for the script.
REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SET ps_script="%me%.ps1"

REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM :: Main script logic.
REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

powershell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -Verb RunAs powershell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -NoLogo -File \"%parent%\%ps_script%\"'"

REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM :: Exit paths should point to here.
REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:done
IF %errno% NEQ 0 (
ECHO %me%: Script complete with errors.
PAUSE
) ELSE (
ECHO %me%: Script completed successfully.
)

EXIT /B %errno%
57 changes: 57 additions & 0 deletions bin/add-host.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#Requires -RunAsAdministrator

Param(
[Parameter(Mandatory=$false)]
$wslHost
)

if (!$PSBoundParameters.ContainsKey('wslHost')) {
$wslHost = Read-Host -Prompt 'Enter new hostname to add'
Write-Host ""
}

$file = $env:windir + '\System32\drivers\etc\hosts'

# Get the current IP address of the wsl instance.
$wslIp = $(wsl -e hostname -I)

# Read the last byte of the file.
$size = 1
$buffer = new-object Byte[] $size
$fs = [IO.File]::OpenRead($file)
$fs.Seek(-$size, [System.IO.SeekOrigin]::End) | Out-Null
$fs.Read($buffer, 0, $size) | Out-Null
$fs.Close()

Write-Host "Adding new hosts entry for host [$wslHost]."

# If the file doesn't end with a newline (chr 10), add a newline.
if ($buffer[0] -ne 10) {
Add-Content -Path $file -Value ""
}

# Add the new host line.
Add-Content -Path $file -Value "$wslIp`t$wslHost"

Write-Host 'hosts file updated.'

# If running in the console, wait for input before closing.
if ($Host.Name -eq "ConsoleHost") {
Write-Host ""
Write-Host "Press any key to continue or wait 3 seconds..."

# Make sure buffered input doesn't "press a key" and skip the ReadKey().
$Host.UI.RawUI.FlushInputBuffer()

# Wait for a set time or until a key is pressed.
$counter = 0
while (!$Host.UI.RawUI.KeyAvailable -and $counter -lt 3000) {
Start-Sleep -Milliseconds 100
$counter = $counter + 100
}

# Consume the key if one was pressed.
if ($Host.UI.RawUI.KeyAvailable) {
$Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyUp") > $null
}
}

0 comments on commit 39c36dc

Please sign in to comment.