@ledsun blog

無味の味は佳境に入らざればすなわち知れず

WSL向けのWindowsポートフォワード設定スクリプト new

WSL向けのWindowsポートフォワード設定スクリプトが動かなくなった - @ledsun blog のでバッチファイルをやめてPowerShellで書き直しました。

$port = 8000

if (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole("Administrators"))
{
  # Start portforward
  $ip = wsl ip addr show eth0 | Select-String -Pattern 'inet\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' | ForEach-Object { $_.Matches[0].Groups[1].Value }
  netsh interface portproxy add v4tov4 listenport=$port connectaddress=$ip connectport=$port
  netsh interface portproxy show v4tov4

  # Display the IP addresses of the computer
  $WiFiIPInfo = Get-NetIPAddress -InterfaceAlias "Wi-Fi" -AddressFamily IPv4
  $WiFiIPInfo | ForEach-Object { $_.IPAddress }

  Read-Host "Press Enter to continue..."

  # Stop portforward
  netsh interface portproxy delete v4tov4 $port
}
else {
  Start-Process powershell.exe "-File `"$PSCommandPath`"" -Verb RunAs
}

ロジックは既存のままです。 次の項目をPowerShellで実現する方法をChatGPTに聞きました。

また、PowerShellスクリプトは右クリックから「管理者権限で実行」が出来ませんでした。 こちらは昔ながらのググって解決しました。

参考