-
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 update the hosts file wi…
…th the WSL ip address.
- Loading branch information
1 parent
098590e
commit 90e5cd0
Showing
2 changed files
with
80 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,39 @@ | ||
#Requires -RunAsAdministrator | ||
|
||
# Any host line that contains this TLD will have the IP address updated. | ||
$wslTld = 'test' | ||
|
||
$file = $env:windir + '\System32\drivers\etc\hosts' | ||
$regex = '(?<=^\s*)([^\s]+)(?=.*\.' + $wslTld + '(\s+|$))' | ||
|
||
# Get the current IP address of the wsl instance. | ||
$wslIp = $(wsl -e hostname -I).Trim() | ||
|
||
Write-Host "Updating hosts file for TLD [.$wslTld] with WSL IP [$wslIp]." | ||
|
||
# Use the regex to replace existing IP addresses with the | ||
# current wsl IP address for the wsl TLD defined above. | ||
(Get-Content $file) -replace $regex, $wslIp | Set-Content $file | ||
|
||
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 | ||
} | ||
} |