Fixing pwsh to wsl docker setup

This commit is contained in:
Zackarias Montell
2026-08-11 10:48:01 +02:00
parent e850b16df6
commit 4ac2428755
2 changed files with 201 additions and 50 deletions

View File

@@ -3,13 +3,20 @@
# Usage: . .\Update-WSLDockerProxy.ps1 (dot-source to import the function)
# Update-WSLDockerProxy (then call it)
# Or just run the script directly and it will execute automatically.
function Update-WSLDockerProxy {
[CmdletBinding()]
param(
[int]$Port = 2375
[int]$Port = 2375,
[string]$LogPath = "$env:TEMP\Update-WSLDockerProxy.log"
)
function Write-Log {
param([string]$Message)
$line = "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') $Message"
Write-Host $Message
Add-Content -Path $LogPath -Value $line
}
# Self-elevate if not running as admin
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "Not running as admin, re-launching elevated..." -ForegroundColor Yellow
@@ -22,27 +29,38 @@ function Update-WSLDockerProxy {
return
}
# Get current WSL IP
$WSL_IP = (wsl hostname -I 2>$null).Split(" ")[0].Trim()
# --- Get current WSL IP ---
# wsl.exe emits UTF-16 output with embedded null bytes when its stdout isn't
# attached to a real console (hidden window, scheduled task, self-elevated
# relaunch, etc). That corrupts naive string parsing (e.g. results in "W"
# instead of an IP). Strip null chars before parsing, and validate the
# result looks like an actual IPv4 address before using it.
$rawOutput = (wsl.exe hostname -I 2>$null) -join ' '
$cleaned = ($rawOutput -replace "`0", '').Trim()
$WSL_IP = $null
if ($cleaned) {
$candidates = $cleaned -split '\s+' | Where-Object { $_ -match '^\d{1,3}(\.\d{1,3}){3}$' }
$WSL_IP = $candidates | Select-Object -First 1
}
if (-not $WSL_IP) {
Write-Error "Could not get WSL IP. Is WSL running?"
Write-Log "ERROR: Could not get a valid WSL IP. Raw output was: '$rawOutput'"
return
}
Write-Host "WSL IP: $WSL_IP" -ForegroundColor Cyan
Write-Log "WSL IP: $WSL_IP"
# Remove existing rule if present
$existing = netsh interface portproxy show v4tov4 | Select-String ":$Port"
if ($existing) {
Write-Host "Removing existing portproxy rule on port $Port..." -ForegroundColor Gray
Write-Log "Removing existing portproxy rule on port $Port..."
netsh interface portproxy delete v4tov4 listenport=$Port listenaddress=127.0.0.1 | Out-Null
}
# Add new rule
netsh interface portproxy add v4tov4 listenport=$Port listenaddress=127.0.0.1 connectport=$Port connectaddress=$WSL_IP | Out-Null
Write-Host "Portproxy updated: 127.0.0.1:$Port -> ${WSL_IP}:$Port" -ForegroundColor Green
Write-Log "Portproxy updated: 127.0.0.1:$Port -> ${WSL_IP}:$Port"
}
# Run automatically if script is executed directly (not dot-sourced)