|
8 | 8 | #> |
9 | 9 |
|
10 | 10 | # ==== Script version ==== |
11 | | -$scriptVersion = "1.0.17" |
| 11 | +$scriptVersion = "1.0.18" |
12 | 12 |
|
13 | 13 | # ==== Configuration ==== |
14 | 14 | $configFile = "$PSScriptRoot\adapter.config" |
15 | 15 | $logFile = $true |
16 | 16 | $logReverse = $false |
17 | 17 | $logFilePath = "$PSScriptRoot\script.log" |
18 | | - $restartWiFi = $false |
| 18 | + $restartWiFi = $true |
19 | 19 | $forceAsAdmin = $true |
20 | | - $delay = 30 |
| 20 | + $delayViaScript = $true |
| 21 | + $delay = 90 |
21 | 22 |
|
22 | 23 | # FORCE POWERSHELL 5.1 HANDOFF (Must come first to avoid Type errors in PS7) |
23 | 24 | if ($PSVersionTable.PSVersion.Major -gt 5) { |
@@ -159,6 +160,26 @@ try { |
159 | 160 | return $false |
160 | 161 | } |
161 | 162 |
|
| 163 | + # Function to stop/start WireGuard tunnels to prevent registry bloat |
| 164 | + function Set-WireGuardTunnel { |
| 165 | + param ([string]$State) # "On" or "Off" |
| 166 | + |
| 167 | + $wgPath = "C:\Program Files\WireGuard\wireguard.exe" |
| 168 | + if (-not (Test-Path $wgPath)) { return } |
| 169 | + |
| 170 | + # Identify active tunnels |
| 171 | + # WireGuard often stores tunnel names in the registry or you can specify your tunnel name |
| 172 | + $tunnelName = "YourTunnelName" # Replace with your actual tunnel name |
| 173 | + |
| 174 | + if ($State -eq "Off") { |
| 175 | + LogThis "Closing WireGuard tunnel to prevent stale connections..." -Color "Yellow" |
| 176 | + Start-Process -FilePath $wgPath -ArgumentList "/uninstalltunnelservice $tunnelName" -Wait -WindowStyle Hidden |
| 177 | + } else { |
| 178 | + LogThis "Re-activating WireGuard tunnel..." -Color "Cyan" |
| 179 | + Start-Process -FilePath $wgPath -ArgumentList "/installtunnelservice $tunnelName" -Wait -WindowStyle Hidden |
| 180 | + } |
| 181 | + } |
| 182 | + |
162 | 183 | # Main Hotspot Toggle Logic |
163 | 184 | function Switch-Hotspot { |
164 | 185 | param($adapter) |
@@ -190,6 +211,9 @@ try { |
190 | 211 | return $true |
191 | 212 | } |
192 | 213 |
|
| 214 | + # Pre-reset cleanup |
| 215 | + Set-WireGuardTunnel -State "Off" |
| 216 | + |
193 | 217 | LogThis "Resetting Radio for Win10 Routing Fix..." -Color "Cyan" |
194 | 218 | Set-WifiRadioState -TargetState "Off" |
195 | 219 | Start-Sleep -Seconds 2 |
@@ -222,14 +246,19 @@ if ($forceAsAdmin) { RunAsAdmin } |
222 | 246 |
|
223 | 247 | $targetAdapter = Get-WiFiAdapter |
224 | 248 |
|
225 | | -if (-not (IsRunningFromTerminal)) { |
226 | | - LogThis "Background mode: Delaying $delay sec for system stability..." |
| 249 | +# You can delay here, or via Task Scheduler |
| 250 | +if ($delayViaScript -and -not (IsRunningFromTerminal)) { |
| 251 | + |
| 252 | + LogThis "Background mode: Delaying $delay seconds." |
227 | 253 | Start-Sleep -Seconds $delay |
| 254 | + |
228 | 255 | } |
229 | 256 |
|
| 257 | +# Main Hotspot Toggle |
230 | 258 | Switch-Hotspot -adapter $targetAdapter |
231 | 259 | LogThis "==== Done ====" |
232 | 260 |
|
233 | 261 |
|
234 | 262 |
|
235 | 263 |
|
| 264 | + |
0 commit comments