Skip to content

Instantly share code, notes, and snippets.

@ErvalhouS
Last active February 4, 2021 12:45
Show Gist options
  • Save ErvalhouS/d869c86083bb598ad52e630c7b1fb7ca to your computer and use it in GitHub Desktop.
Save ErvalhouS/d869c86083bb598ad52e630c7b1fb7ca to your computer and use it in GitHub Desktop.
This gist makes it possible to maintain `.test` domains working on your windows + WSL setup
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs
exit
}
$DesiredIP = ((bash.exe -c "hostname -I") | Out-String) -replace '\s',''
$domains = "hubstaff.test","account.hubstaff.test","app.hubstaff.test","tasks.hubstaff.test","talent.hubstaff.test"
$hostsFilePath = "$($Env:WinDir)\system32\Drivers\etc\hosts"
$hostsFile = Get-Content -path $hostsFilePath -raw
if ($hostsFile -notmatch "`r`n$") {
Add-Content $hostsFilePath "`r`n"
}
foreach($hostname in $domains) {
$escapedHostname = [Regex]::Escape($hostname)
$patternToMatch = ".* $escapedHostname"
if ($hostsFile -match ".* $escapedHostname") {
Write-Host "Replacing existing record for $hostname"
($hostsFile -replace ".* $escapedHostname","$DesiredIP $hostname") | Out-File -NoNewline $hostsFilePath
$hostsFile = Get-Content -path $hostsFilePath -raw
} else {
Write-Host "Adding record for $hostname"
Add-Content $hostsFilePath "$DesiredIP $hostname"
}
}
Write-Host "Done :)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment