-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a powershell script with batch helper to add a new host to the ho…
…sts file.
- Loading branch information
1 parent
90e5cd0
commit 39c36dc
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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% |
This file contains 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
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 | ||
} | ||
} |