1
+ Set-StrictMode - Version 2
2
+ $ErrorActionPreference = ' Stop'
3
+
4
+ try {
5
+ New-VMSwitch - name MultipassSwitch - NetAdapterName Ethernet - AllowManagementOS $true
6
+ }
7
+ catch {
8
+ if ($_.Exception.Message -notmatch " already bound" ) {
9
+ Write-Output $_
10
+ exit 1
11
+ }
12
+ }
13
+
14
+ $hostname = $env: COMPUTERNAME.ToLower ()
15
+ $multipassdPort = 51000
16
+ $env: MULTIPASS_SERVER_ADDRESS = " $ ( $hostname ) :$ ( $multipassdPort ) "
17
+ Write-Output " Set MULTIPASS_SERVER_ADDRESS=$ ( $env: MULTIPASS_SERVER_ADDRESS ) "
18
+
19
+ $binPathValue = " C:\Program Files\Multipass\bin\multipassd.exe /svc --verbosity debug --logger stderr --address $ ( $env: MULTIPASS_SERVER_ADDRESS ) "
20
+ Write-Output " Reconfiguring multipassd to binPath=$binPathValue "
21
+ & ' C:\Windows\System32\sc.exe' config " Multipass" binPath= $binPathValue start= delayed- auto
22
+
23
+ Write-Output " Restarting multipassd"
24
+ Restart-Service - Name " Multipass"
25
+
26
+ function Wait-ForCommand {
27
+ param (
28
+ [ScriptBlock ]$commandScriptBlock ,
29
+ [int ]$timeoutSeconds = 2 ,
30
+ [int ]$maxAttempts = 10
31
+ )
32
+
33
+ $attempts = 0
34
+
35
+ while ($true ) {
36
+ $job = Start-Job - ScriptBlock $commandScriptBlock
37
+ Wait-Job $job - Timeout $timeoutSeconds > $null 2>&1
38
+ $result = Receive-Job $job
39
+ Remove-Job - Force $job
40
+
41
+ if ($null -eq $result ) {
42
+ Write-Output " Command timed out after $timeoutSeconds seconds"
43
+ }
44
+ else {
45
+ if ([string ]$result -eq " 0" ) {
46
+ return
47
+ }
48
+ }
49
+
50
+ if ($attempts -gt $maxAttempts ) {
51
+ throw " Command failed to execute properly"
52
+ }
53
+ else {
54
+ Write-Host " Still waiting for command to complete..."
55
+ Start-Sleep - s 1
56
+ $attempts += 1
57
+ }
58
+ }
59
+ }
60
+
61
+ function Wait-ForMultipassd {
62
+ param (
63
+ [int ]$timeoutSeconds = 2 ,
64
+ [int ]$maxAttempts = 10
65
+ )
66
+ Write-Output " Waiting for multipassd to start"
67
+ $scriptBlock = {
68
+ $version = (multipass.exe version)
69
+ if ($LASTEXITCODE -eq 0 -and $version -notmatch " multipassd" ) {
70
+ return 0
71
+ }
72
+ return 1
73
+ }
74
+ Wait-ForCommand - commandScriptBlock $scriptBlock - timeoutSeconds $timeoutSeconds - maxAttempts $maxAttempts
75
+ }
76
+
77
+ Wait-ForMultipassd
78
+
79
+ # Write-Output "Setting local.passphrase"
80
+ # multipass.exe set local.passphrase=foo
81
+ # if ($LASTEXITCODE -ne 0) {
82
+ # Write-Output "Failed to set local.passphrase"
83
+ # exit $LASTEXITCODE
84
+ # }
85
+
86
+ # Wait-ForMultipassd
87
+ # Write-Output "Setting local.bridged-network"
88
+ # multipass.exe set local.bridged-network=MultipassSwitch
89
+ # if ($LASTEXITCODE -ne 0) {
90
+ # Write-Output "Failed to set local.bridged-network"
91
+ # exit $LASTEXITCODE
92
+ # }
93
+
94
+ # # # Wait-ForMultipassd
95
+ # Write-Output "Getting ethernet IP"
96
+ # try {
97
+ # $ethIp = (Get-NetIPAddress -InterfaceAlias "vEthernet (MultipassSwitch)" -AddressFamily IPv4).IPAddress
98
+ # }
99
+ # catch {
100
+ # Write-Output "Failed to get ethernet IP"
101
+ # Write-Output $_
102
+ # exit 1
103
+ # }
104
+
105
+ # Write-Output "Found ethernet IP: $ethIp"
106
+
107
+ # Write-Output "Going to add portproxy rule for $($ethIp):$($multipassdPort)"
108
+ # netsh interface portproxy add v4tov4 listenport=$multipassdPort listenaddress=$ethIp connectport=$multipassdPort connectaddress=127.0.0.1
109
+ # if ($LASTEXITCODE -ne 0) {
110
+ # exit $LASTEXITCODE
111
+ # }
112
+
113
+ # Write-Output "Going to add firewall rule for $($ethIp):$($multipassdPort)"
114
+ # New-NetFirewallRule -DisplayName "multipassd_51000" -Direction Inbound -Protocol TCP -LocalPort $multipassdPort -Action Allow -PolicyStore PersistentStore
115
+ # if ($LASTEXITCODE -ne 0) {
116
+ # exit $LASTEXITCODE
117
+ # }
0 commit comments