Last active
September 4, 2024 12:19
-
-
Save anonhostpi/fe3c88c8371b0a495ff39942d8aeadac to your computer and use it in GitHub Desktop.
WSL Network Adapter Mapper
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
# https://gist.github.com/anonhostpi/fe3c88c8371b0a495ff39942d8aeadac | |
function global:Get-WSLNetMapping { | |
# Notify user if Mirroring Mode is not enabled | |
param( | |
[Alias("d")] | |
$Distribution | |
) | |
& { | |
Try { | |
$wslconfig = Resolve-Path "$env:UserProfile\.wslconfig" -ErrorAction SilentlyContinue | |
if( $wslconfig ){ | |
} Else { | |
throw | |
} | |
$wslconfig = $wslconfig | Get-Content | |
if( $wslconfig | Select-String "networkingMode=mirrored" ){ | |
} else { | |
Write-Warning "Mirroring mode is not enabled!" | |
} | |
} Catch { | |
Write-Warning "$env:UserProfile\.wslconfig has not been created!" | |
Write-Warning "Mirroring mode is not enabled!" | |
} | |
} | |
Write-Host "Getting adapter data..." | |
$sources = @{} | |
$sources.win = Get-NetAdapter -IncludeHidden | |
$sources.wsl = If( $Distribution ){ | |
wsl -d $Distribution -- "ip" "-json" "a" | ConvertFrom-Json | |
} Else { | |
wsl -- "ip" "-json" "a" | ConvertFrom-Json | |
} | |
# Ammend the IP address information on the Windows Adapters | |
& { | |
$addresses = Get-NetIPAddress | |
$sources.win | ForEach-Object { | |
$win = $_ | |
$win | Add-Member ` | |
-MemberType NoteProperty ` | |
-Name "AddressInfo" ` | |
-Value ($addresses | Where-Object { | |
$_.InterfaceIndex -eq $win.ifIndex | |
}) | |
} | |
} | |
Write-Host "Parsing adapter data..." | |
$sources.wsl | ForEach-Object { | |
$wsl = $_ | |
$win = $sources.win | Where-Object { | |
$macs = @{ | |
"wsl" = $wsl.address -replace ":","" | |
"win" = $_.MacAddress -replace "-","" | |
} | |
$macs.wsl -ieq $macs.win | |
} | |
New-Object PSobject -Property ([ordered]@{ | |
"Linux" = $wsl.ifname | |
"Windows" = $win.Name | |
"MAC (Linux)" = $wsl.address | |
"MAC (Windows)" = $win.MacAddress | |
"IPv4 (Linux)" = $wsl.addr_info | Where-Object { | |
$_.family -eq "inet" | |
} | Select-Object -ExpandProperty "local" | |
"IPv4 (Windows)" = $win.AddressInfo | Where-Object { | |
$_.AddressFamily -eq "IPv4" | |
} | |
"IPv6 (Linux)" = $wsl.addr_info | Where-Object { | |
$_.family -eq "inet6" | |
} | Select-Object -ExpandProperty "local" | |
"IPv6 (Windows)" = $win.AddressInfo | Where-Object { | |
$_.AddressFamily -eq "IPv6" | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment