-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.ps1
75 lines (63 loc) · 2.2 KB
/
run.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Function to get the default network adapter name and gateway IP
function Get-NetworkInfo {
$ipconfig = ipconfig
$adapter = $null
$gateway = $null
foreach ($line in $ipconfig) {
if ($line -match "Ethernet adapter (.+):") {
$adapter = $matches[1]
}
if ($line -match "Default Gateway[ .]*: ([\d.]+)") {
$gateway = $matches[1]
break
}
}
return $adapter, $gateway
}
# Function to find an available IP address in a given subnet
function Get-AvailableIPAddress {
param (
[string]$Subnet = "192.168.1.",
[int]$StartRange = 100,
[int]$EndRange = 200
)
for ($i = $StartRange; $i -le $EndRange; $i++) {
$ip = $Subnet + $i
if (!(Test-Connection -ComputerName $ip -Count 1 -Quiet)) {
return $ip
}
}
throw "No available IP addresses found in the range $Subnet$StartRange to $Subnet$EndRange"
}
# Function to create a network bridge
function Create-NetworkBridge {
param (
[string]$AdapterName,
[string]$WSLAdapterName = "vEthernet (WSL)",
[string]$BridgeName = "WSLBridge",
[string]$Subnet = "192.168.1.",
[int]$PrefixLength = 24,
[string]$Gateway
)
# Find an available IP address
$IPAddress = Get-AvailableIPAddress -Subnet $Subnet
# Remove existing network bridge if it exists
$existingBridge = Get-NetAdapter -Name $BridgeName -ErrorAction SilentlyContinue
if ($existingBridge) {
Remove-NetSwitchTeam -Name $BridgeName -Confirm:$false
}
# Create a new network bridge
New-NetSwitchTeam -Name $BridgeName -TeamMembers $AdapterName, $WSLAdapterName
# Configure the bridge adapter
New-NetIPAddress -InterfaceAlias $BridgeName -IPAddress $IPAddress -PrefixLength $PrefixLength -DefaultGateway $Gateway
}
# Main script execution
$adapter, $gateway = Get-NetworkInfo
if ($adapter -and $gateway) {
Write-Output "Detected adapter: $adapter"
Write-Output "Detected gateway: $gateway"
Create-NetworkBridge -AdapterName $adapter -Gateway $gateway
Write-Output "WSL2 bridged networking setup complete."
} else {
Write-Output "Failed to detect network information"
}