-
Notifications
You must be signed in to change notification settings - Fork 20
/
resetWindowsNet.ps1
77 lines (52 loc) · 2.61 KB
/
resetWindowsNet.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
76
77
# to execute you need to set this up:
# Set-ExecutionPolicy RemoteSigned
# let's check how boot process is going to be
$global:logPath = "C:\Startup\wsl2_boot.log"
# TODO: configureWSL2Net set global variable with path to shell script to configure WSL network interface inside Linux
# this function is used to configure network settings after VMSwitch is ready to be used by wsl instance
function ConfigureWSLNetwork {
Write-Output "Starting WSL..." >> $logPath
$wslStatus = Get-Process -Name "wsl" -ErrorAction SilentlyContinue
if (!($wslStatus)) {
Start-Job -ScriptBlock { Start-Process -FilePath "wsl.exe" -WindowStyle hidden }
}
Do {
$wslStatus = Get-Process -Name "wsl" -ErrorAction SilentlyContinue
If (!($wslStatus)) { Write-Output 'Waiting for WSL2 process to start' >> $logPath ; Start-Sleep 1 }
Else { Write-Output 'WSL Process has started, configuring network' >> $logPath ; $wslStarted = $true }
}
Until ( $wslStarted )
$wslStatus 5>> $logPath
# wsl --distribution Ubuntu-20.04 -u root /home/p/configureWSL2Net.sh
# configureWSL2Net.sh needs to be made executable
Start-Process -FilePath "wsl.exe" -ArgumentList "-u root /home/p/configureWSL2Net.sh"
Write-Output "network configuration completed" >> $logPath
Write-Output $wslStatus 5>> $logPath
return 0
}
# force launch without going to bash prompt
wsl exit
wsl -l -v *>> $logPath
$started = $false
Do {
$status = Get-VMSwitch WSL -ErrorAction SilentlyContinue
Write-Output $status >> $logPath
If (!($status)) { Write-Output 'Waiting for WSL swtich to get registered' ; Start-Sleep 1 }
Else {
Write-Output "WSL Network found" ;
$started = $true;
# manipulate network adapter tickboxes - Adapter cannot be bound because binding to Hyper-V is still there after M$ windows restarts.
# Get-NetAdapterBinding Ethernet to view components of the interface vms_pp is what we look for
Set-NetAdapterBinding -Name "Ethernet" -ComponentID vms_pp -Enabled $False ;
Set-VMSwitch WSL -NetAdapterName "Ethernet" ;
$started = $true ;
# Hook all Hyper V VMs to WSL network => avoid network performance issues.
Write-Output "Getting all Hyper V machines to use WSL Switch" >> $logPath ;
Get-VM | Get-VMNetworkAdapter | Connect-VMNetworkAdapter -SwitchName "WSL" ;
# now that host network is configured we can set up wsl network
ConfigureWSLNetwork ;
# Start All Hyper VMs
Get-VM | Start-VM ;
}
}
Until ( $started )