Skip to content

Instantly share code, notes, and snippets.

@kakkun61
Forked from MichaelBelgium/wsl2-hosts-sync.ps1
Last active August 10, 2022 02:50
Show Gist options
  • Save kakkun61/2139c240ac0dce1bb1606c1e8bc8038c to your computer and use it in GitHub Desktop.
Save kakkun61/2139c240ac0dce1bb1606c1e8bc8038c to your computer and use it in GitHub Desktop.
WSL v2 windows hosts sync (powershell - updates domains to wsl2 ip)
If (!(New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host 'Run this as administrator.'
$choice = Read-Host 'Do you want to re-run this as administrator? [y/N]'
if ($choice -ne 'y') {
exit 1
}
$actualScriptRoot = $PSScriptRoot
if ((Get-Item $PSCommandPath).Target) {
$actualScriptRoot = Split-Path -Parent ((Get-Item $PSCommandPath).Target)
}
Start-Process `
-FilePath pwsh `
-ArgumentList "$actualScriptRoot\wsl-hosts.ps1" `
-Verb RunAs `
-Wait
exit 0
}
if ($null -eq (Get-InstalledModule "Carbon" -ErrorAction SilentlyContinue)) {
Write-Host '"Carbon" module is necessary and it is not installed.'
$choice = Read-Host 'Do you want to install it? [y/N]'
if ($choice -ne 'y') {
exit 1
}
Install-Module -Name 'Carbon' -AllowClobber
Import-Module 'Carbon'
}
$wslIp = (wsl -- ip addr show eth0 '|' grep 'inet ' '|' cut -f 6 -d ' ' '|' cut -f 1 -d '/')
Remove-CHostsEntry -HostName 'wsl.local'
Set-CHostsEntry -IPAddress $wslIp -HostName 'wsl.local'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment