-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdate-LSOAndTest.ps1
More file actions
267 lines (219 loc) · 9.58 KB
/
Update-LSOAndTest.ps1
File metadata and controls
267 lines (219 loc) · 9.58 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<#
MIT License
Copyright (c) 2025 noderaven
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
.SYNOPSIS
Disables Large Send Offload (LSO) on active network adapters, tests connectivity,
and attempts remediation steps if the network is down.
.DESCRIPTION
This script performs a comprehensive check and remediation for network connectivity issues
potentially caused by Large Send Offload (LSO) settings. It is designed for safe,
non-interactive remote execution with detailed output by default.
It gracefully handles differences between Windows PowerShell and PowerShell Core (6+)
to ensure broad compatibility.
1. Ensures it is run with Administrator privileges.
2. Filters for physical network adapters that are currently 'Up'.
3. Disables LSO v2 for both IPv4 and IPv6 on these adapters.
4. If no changes are made, the script exits, assuming the network is healthy.
5. If a change was made, it waits and then tests internet connectivity.
6. If the test fails, it restarts the network adapters and tests again.
7. If the second test fails, it recommends a system restart. If the -ForceReboot
switch is used, it will automatically restart the computer.
.PARAMETER PingTargets
An array of IP addresses or hostnames to use for network connectivity tests.
Defaults to Google's and Cloudflare's public DNS servers.
.PARAMETER InitialWaitSeconds
The number of seconds to wait after disabling LSO before the first network test.
Defaults to 45 seconds.
.PARAMETER ReinitializeWaitSeconds
The number of seconds to wait after restarting adapters before the second network test.
Defaults to 30 seconds.
.PARAMETER ForceReboot
A switch parameter. If present, the script will force a computer restart if the
second network connectivity test fails. Otherwise, it will only write a warning.
.EXAMPLE
PS C:\> .\Update-LSOAndTest.ps1
Executes the script with detailed output.
.EXAMPLE
PS C:\> .\Update-LSOAndTest.ps1 -ForceReboot
Executes the script and will force a restart if network remediation fails. This is non-interactive.
#>
#requires -RunAsAdministrator
#requires -Version 5.1
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[string[]]$PingTargets = @('8.8.8.8', '1.1.1.1'),
[int]$InitialWaitSeconds = 45,
[int]$ReinitializeWaitSeconds = 30,
[switch]$ForceReboot
)
# --- Helper Functions ---
function Set-LSOState {
<#
.SYNOPSIS
A helper function to enable or disable a specific LSO property on an adapter.
.DESCRIPTION
This function encapsulates the logic for getting and setting an advanced property
on a network adapter, reducing code duplication.
.OUTPUTS
[boolean] Returns $true if a change was made, $false otherwise.
#>
[CmdletBinding()]
[OutputType([boolean])]
param(
[Parameter(Mandatory = $true)]
[psobject]$Adapter,
[Parameter(Mandatory = $true)]
[string]$PropertyName,
[Parameter(Mandatory = $true)]
[string]$PropertyValue
)
Write-Host "Processing property '$($PropertyName)' for adapter '$($Adapter.Name)'"
$getParams = @{ Name = $Adapter.Name; DisplayName = $PropertyName; ErrorAction = 'SilentlyContinue' }
$advancedProperty = Get-NetAdapterAdvancedProperty @getParams
if ($null -ne $advancedProperty) {
if ($advancedProperty.DisplayValue -eq $PropertyValue) {
Write-Host "[$($Adapter.Name)] '$($PropertyName)' is already set to '$($PropertyValue)'."
return $false # No change made
}
try {
$setParams = @{
Name = $Adapter.Name
DisplayName = $PropertyName
DisplayValue = $PropertyValue
ErrorAction = 'Stop'
Confirm = $false
}
if ($PSCmdlet.ShouldProcess("adapter '$($Adapter.Name)'", "Set advanced property '$($PropertyName)' to '$($PropertyValue)'")) {
Set-NetAdapterAdvancedProperty @setParams
Write-Host "[$($Adapter.Name)] Successfully set '$($PropertyName)' to '$($PropertyValue)'."
return $true # Change was made
}
}
catch {
Write-Warning "[$($Adapter.Name)] Failed to set '$($PropertyName)'. Error: $_"
}
}
else {
Write-Host "[$($Adapter.Name)] Advanced property '$($PropertyName)' not found. Skipping."
}
# Default return if no other path was taken (e.g., property not found, error, -WhatIf)
return $false
}
function Test-RobustNetworkConnection {
<#
.SYNOPSIS
Tests network connectivity by pinging one or more targets.
.DESCRIPTION
Uses Test-Connection, the PowerShell-native way to ping. It is more robust
as it will return $true if ANY of the targets respond successfully.
It automatically uses the correct parameter name (-ComputerName or -TargetName)
based on the running PowerShell version.
#>
[CmdletBinding()]
[OutputType([boolean])]
param(
[Parameter(Mandatory = $true)]
[string[]]$Targets
)
foreach ($target in $Targets) {
Write-Host "Pinging $target..."
# Use a splatting table to hold the parameters for Test-Connection
$testConnectionParams = @{
Count = 1
ErrorAction = 'SilentlyContinue'
Quiet = $true
}
# Add the correct parameter name based on the PowerShell version.
# Windows PowerShell (e.g., v5.1) uses -ComputerName.
# PowerShell Core (v6+) uses -TargetName.
if ($PSVersionTable.PSVersion.Major -lt 6) {
$testConnectionParams['ComputerName'] = $target
}
else {
$testConnectionParams['TargetName'] = $target
}
if (Test-Connection @testConnectionParams) {
Write-Host "Ping to $target was successful."
return $true
}
Write-Host "Ping to $target failed."
}
return $false
}
# --- Main Script Logic ---
Write-Host "Searching for active, physical network adapters..."
$adapters = Get-NetAdapter -Physical | Where-Object { $_.Status -eq 'Up' }
if ($null -eq $adapters) {
Write-Warning "No active, physical network adapters found. Exiting script."
exit 1
}
Write-Host "Found $($adapters.Count) active adapter(s): $($adapters.Name -join ', ')"
$anyChangesMade = $false
foreach ($adapter in $adapters) {
if (Set-LSOState -Adapter $adapter -PropertyName "Large Send Offload V2 (IPv4)" -PropertyValue "Disabled") {
$anyChangesMade = $true
}
if (Set-LSOState -Adapter $adapter -PropertyName "Large Send Offload V2 (IPv6)" -PropertyValue "Disabled") {
$anyChangesMade = $true
}
}
# --- Conditional Logic based on changes ---
if (-not $anyChangesMade) {
Write-Host "No LSO changes were necessary. Assuming network is in a healthy state. Exiting."
exit 0
}
# --- Proceed with testing only if changes were made ---
Write-Host "Waiting for $InitialWaitSeconds seconds for network to stabilize after changes..."
Start-Sleep -Seconds $InitialWaitSeconds
Write-Host "Performing first network connection test..."
if (Test-RobustNetworkConnection -Targets $PingTargets) {
Write-Host "Network connection is ACTIVE. Script finished successfully."
exit 0
}
# --- Remediation Steps ---
Write-Warning "First network test FAILED. Attempting to restart network adapters."
foreach ($adapter in $adapters) {
try {
if ($PSCmdlet.ShouldProcess($adapter.Name, "Restart-NetAdapter")) {
Restart-NetAdapter -Name $adapter.Name -ErrorAction Stop -Confirm:$false
Write-Host "Successfully restarted adapter: $($adapter.Name)"
}
}
catch {
Write-Warning "Failed to restart adapter '$($adapter.Name)'. Error: $_"
}
}
Write-Host "Waiting $ReinitializeWaitSeconds seconds for adapters to reinitialize."
Start-Sleep -Seconds $ReinitializeWaitSeconds
Write-Host "Performing second network connection test..."
if (Test-RobustNetworkConnection -Targets $PingTargets) {
Write-Host "Network connection is ACTIVE after restarting adapters. Script finished successfully."
exit 0
}
# --- Final Step ---
Write-Warning "Second network test FAILED. Further manual intervention may be required."
if ($ForceReboot) {
if ($PSCmdlet.ShouldProcess("the local computer", "Restart-Computer -Force")) {
Write-Warning "Restarting the computer in 10 seconds due to -ForceReboot switch..."
Start-Sleep -Seconds 10
Restart-Computer -Force
}
}
else {
Write-Host "To automatically restart, run this script again with the -ForceReboot switch."
}