Repository navigation
Expand file tree
/
Copy pathEthernetScript
More file actions
515 lines (397 loc) · 14.4 KB
/
Copy pathEthernetScript
File metadata and controls
515 lines (397 loc) · 14.4 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
& {
$ErrorActionPreference = "Stop"
$adapter = "Ethernet"
# Administrator check
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($identity)
if (-not $principal.IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "Run as Administrator." -ForegroundColor Red
return
}
$index = (Get-NetAdapter -Name $adapter).InterfaceIndex
function Read-Mode {
param([string]$Family)
Write-Host "`n===== $Family CONFIGURATION =====" -ForegroundColor Cyan
Write-Host "1. Automatic"
Write-Host "2. Manual only - no automatic assignment"
Write-Host "3. Disabled - completely turn off"
Write-Host "4. Keep unchanged"
do {
$mode = Read-Host "Select option (1-4)"
} until ($mode -in @("1","2","3","4"))
return $mode
}
function Read-Address {
param(
[string]$Prompt,
[string]$Family,
[bool]$Required = $false
)
while ($true) {
$value = (Read-Host $Prompt).Trim()
if (!$value -and !$Required) {
return ""
}
$parsed = $null
if ([System.Net.IPAddress]::TryParse($value, [ref]$parsed)) {
$type = $parsed.AddressFamily.ToString()
if (($Family -eq "IPv4" -and $type -eq "InterNetwork") -or
($Family -eq "IPv6" -and $type -eq "InterNetworkV6")) {
if (![System.Net.IPAddress]::IsLoopback($parsed) -and
$parsed.ToString() -notin @("0.0.0.0","::")) {
return $parsed.ToString()
}
}
}
Write-Host "Enter a valid $Family address." -ForegroundColor Red
}
}
function Read-Prefix {
param([int]$Maximum, [int]$Default)
while ($true) {
$value = Read-Host "Prefix length (Enter = $Default)"
if (!$value) {
return $Default
}
$number = 0
if ([int]::TryParse($value, [ref]$number) -and
$number -ge 1 -and $number -le $Maximum) {
return $number
}
Write-Host "Invalid prefix length." -ForegroundColor Red
}
}
function Run-Netsh {
param([string[]]$Arguments)
& netsh.exe @Arguments
if ($LASTEXITCODE -ne 0) {
throw "Command failed: netsh $($Arguments -join ' ')"
}
}
function Set-ProtocolBinding {
param([string]$Component, [bool]$Enabled)
$binding = Get-NetAdapterBinding `
-Name $adapter -ComponentID $Component
if ($binding.Enabled -ne $Enabled) {
Set-NetAdapterBinding `
-Name $adapter `
-ComponentID $Component `
-Enabled $Enabled
}
}
function Remove-OldAddresses {
param(
[string]$Family,
[string]$KeepAddress = "",
[bool]$ManualOnly = $false
)
foreach ($store in @("PersistentStore","ActiveStore")) {
$addresses = Get-NetIPAddress `
-InterfaceIndex $index `
-AddressFamily $Family `
-PolicyStore $store `
-ErrorAction SilentlyContinue
foreach ($entry in $addresses) {
if ($entry.IPAddress -eq $KeepAddress) {
continue
}
if ($Family -eq "IPv6" -and
([System.Net.IPAddress]::Parse(
$entry.IPAddress)).IsIPv6LinkLocal) {
continue
}
if ($ManualOnly -and $entry.PrefixOrigin -ne "Manual") {
continue
}
Write-Host "Removing $($entry.IPAddress)" `
-ForegroundColor Yellow
Remove-NetIPAddress `
-InterfaceIndex $index `
-IPAddress $entry.IPAddress `
-PolicyStore $store `
-Confirm:$false
}
}
}
function Remove-OldGateways {
param(
[string]$Family,
[string]$KeepGateway = "",
[bool]$ManualOnly = $false
)
$destination = if ($Family -eq "IPv4") {
"0.0.0.0/0"
} else {
"::/0"
}
foreach ($store in @("PersistentStore","ActiveStore")) {
$routes = Get-NetRoute `
-InterfaceIndex $index `
-AddressFamily $Family `
-PolicyStore $store `
-ErrorAction SilentlyContinue |
Where-Object {
$_.DestinationPrefix -eq $destination
}
foreach ($route in $routes) {
if ($route.NextHop -eq $KeepGateway) {
continue
}
if ($ManualOnly -and $route.Protocol -ne "NetMgmt") {
continue
}
Remove-NetRoute `
-InterfaceIndex $index `
-DestinationPrefix $destination `
-NextHop $route.NextHop `
-PolicyStore $store `
-Confirm:$false
}
}
}
function Configure-DNS {
param(
[string]$Family,
[string]$Mode,
[string]$Primary = "",
[string]$Secondary = ""
)
$protocol = $Family.ToLower()
if ($Mode -eq "1") {
Run-Netsh @(
"interface", $protocol, "set", "dnsservers",
"name=$adapter", "source=dhcp"
)
return
}
$servers = @(
@($Primary, $Secondary) |
Where-Object { $_ } |
Select-Object -Unique
)
if ($servers.Count -eq 0) {
# Empty DNS list, not automatic DNS
Run-Netsh @(
"interface", $protocol, "set", "dnsservers",
"name=$adapter",
"source=static",
"address=none",
"validate=no"
)
} else {
Run-Netsh @(
"interface", $protocol, "set", "dnsservers",
"name=$adapter",
"source=static",
"address=$($servers[0])",
"validate=no"
)
for ($i = 1; $i -lt $servers.Count; $i++) {
Run-Netsh @(
"interface", $protocol, "add", "dnsservers",
"name=$adapter",
"address=$($servers[$i])",
"index=$($i + 1)",
"validate=no"
)
}
}
}
Clear-Host
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " MANUAL NETWORK CONTROL PANEL" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Adapter: $adapter"
Write-Host "Interface index: $index"
# Collect IPv4 settings
$mode4 = Read-Mode "IPv4"
$ip4 = ""
$prefix4 = 24
$gw4 = ""
$dns4a = ""
$dns4b = ""
if ($mode4 -eq "2") {
$ip4 = Read-Address "IPv4Address" "IPv4" $true
$prefix4 = Read-Prefix 32 24
$gw4 = Read-Address "IPv4DefaultGateway (blank = none)" "IPv4"
$dns4a = Read-Address "Primary IPv4 DNS (blank = none)" "IPv4"
$dns4b = Read-Address "Secondary IPv4 DNS (blank = none)" "IPv4"
}
# Collect IPv6 settings
$mode6 = Read-Mode "IPv6"
$ip6 = ""
$prefix6 = 64
$gw6 = ""
$dns6a = ""
$dns6b = ""
if ($mode6 -eq "2") {
$ip6 = Read-Address "IPv6Address" "IPv6" $true
$prefix6 = Read-Prefix 128 64
$gw6 = Read-Address "IPv6DefaultGateway (blank = none)" "IPv6"
$dns6a = Read-Address "Primary IPv6 DNS (blank = none)" "IPv6"
$dns6b = Read-Address "Secondary IPv6 DNS (blank = none)" "IPv6"
}
$modeNames = @{
"1" = "Automatic"
"2" = "Manual only"
"3" = "Disabled"
"4" = "Unchanged"
}
Write-Host "`n===== REVIEW SETTINGS =====" -ForegroundColor Cyan
[PSCustomObject]@{
IPv4Mode = $modeNames[$mode4]
IPv4Address = $ip4
IPv4PrefixLength = $prefix4
IPv4DefaultGateway = $gw4
IPv4DNS1 = $dns4a
IPv4DNS2 = $dns4b
IPv6Mode = $modeNames[$mode6]
IPv6Address = $ip6
IPv6PrefixLength = $prefix6
IPv6DefaultGateway = $gw6
IPv6DNS1 = $dns6a
IPv6DNS2 = $dns6b
} | Format-List
Write-Host "WARNING: Network connectivity may be interrupted." `
-ForegroundColor Yellow
if ($mode4 -eq "3" -and $mode6 -eq "3") {
Write-Host "Both protocols will be disabled." `
-ForegroundColor Red
}
if ((Read-Host "Type APPLY to confirm") -ne "APPLY") {
Write-Host "Cancelled. No changes made."
return
}
# Save previous configuration
$backup = Join-Path $env:USERPROFILE (
"Network-Backup-" + (Get-Date -Format "yyyyMMdd-HHmmss") + ".txt"
)
ipconfig /all | Out-File $backup
netsh interface ipv4 dump | Out-File $backup -Append
netsh interface ipv6 dump | Out-File $backup -Append
Get-NetAdapterBinding -Name $adapter |
Format-Table -AutoSize |
Out-File $backup -Append
Write-Host "Backup saved: $backup"
try {
# Apply IPv4 settings
Write-Host "`n===== APPLYING IPv4 =====" -ForegroundColor Cyan
switch ($mode4) {
"1" {
Set-ProtocolBinding "ms_tcpip" $true
Set-NetIPInterface `
-InterfaceIndex $index `
-AddressFamily IPv4 `
-RouterDiscovery ControlledByDHCP
Run-Netsh @(
"interface", "ipv4", "set", "address",
"name=$adapter",
"source=dhcp",
"store=persistent"
)
Configure-DNS "IPv4" "1"
}
"2" {
Set-ProtocolBinding "ms_tcpip" $true
Set-NetIPInterface `
-InterfaceIndex $index `
-AddressFamily IPv4 `
-Dhcp Disabled `
-RouterDiscovery Disabled
$gateway = if ($gw4) { $gw4 } else { "none" }
Run-Netsh @(
"interface", "ipv4", "set", "address",
"name=$adapter",
"source=static",
"address=$ip4/$prefix4",
"gateway=$gateway",
"store=persistent"
)
# Remove additional addresses, including APIPA
Remove-OldAddresses "IPv4" $ip4
# Remove unwanted default gateways
Remove-OldGateways "IPv4" $gw4
Configure-DNS "IPv4" "2" $dns4a $dns4b
}
"3" {
Set-ProtocolBinding "ms_tcpip" $false
Write-Host "IPv4 disabled." -ForegroundColor Green
}
"4" {
Write-Host "IPv4 unchanged."
}
}
# Apply IPv6 settings
Write-Host "`n===== APPLYING IPv6 =====" -ForegroundColor Cyan
switch ($mode6) {
"1" {
Set-ProtocolBinding "ms_tcpip6" $true
Remove-OldAddresses "IPv6" "" $true
Remove-OldGateways "IPv6" "" $true
Set-NetIPInterface `
-InterfaceIndex $index `
-AddressFamily IPv6 `
-RouterDiscovery Enabled
Configure-DNS "IPv6" "1"
}
"2" {
Set-ProtocolBinding "ms_tcpip6" $true
# Block automatic router configuration
Set-NetIPInterface `
-InterfaceIndex $index `
-AddressFamily IPv6 `
-RouterDiscovery Disabled `
-ManagedAddressConfiguration Disabled `
-OtherStatefulConfiguration Disabled
# Remove old global IPv6 addresses and gateways
Remove-OldGateways "IPv6"
Remove-OldAddresses "IPv6"
New-NetIPAddress `
-InterfaceIndex $index `
-AddressFamily IPv6 `
-IPAddress $ip6 `
-PrefixLength $prefix6 |
Out-Null
if ($gw6) {
New-NetRoute `
-InterfaceIndex $index `
-DestinationPrefix "::/0" `
-NextHop ($gw6 -replace '%.*$','') |
Out-Null
}
Configure-DNS "IPv6" "2" $dns6a $dns6b
}
"3" {
Set-ProtocolBinding "ms_tcpip6" $false
Write-Host "IPv6 disabled." -ForegroundColor Green
}
"4" {
Write-Host "IPv6 unchanged."
}
}
Clear-DnsClientCache
Write-Host "`n===== SETTINGS APPLIED =====" -ForegroundColor Green
} catch {
Write-Host "`nERROR: $_" -ForegroundColor Red
Write-Host "Some settings may already have changed."
Write-Host "Previous configuration: $backup"
}
Write-Host "`n===== PROTOCOL STATUS =====" -ForegroundColor Cyan
Get-NetAdapterBinding -Name $adapter |
Where-Object {
$_.ComponentID -in @("ms_tcpip","ms_tcpip6")
} |
Format-Table DisplayName, ComponentID, Enabled
Write-Host "`n===== CURRENT CONFIGURATION =====" -ForegroundColor Cyan
Get-NetIPConfiguration -InterfaceAlias $adapter
Write-Host "`n===== AUTOMATIC CONFIGURATION STATUS =====" `
-ForegroundColor Cyan
Get-NetIPInterface -InterfaceIndex $index |
Format-Table AddressFamily, Dhcp, RouterDiscovery,
ManagedAddressConfiguration, OtherStatefulConfiguration
Write-Host "`n===== ALL ASSIGNED ADDRESSES =====" -ForegroundColor Cyan
Get-NetIPAddress -InterfaceIndex $index |
Format-Table IPAddress, PrefixLength, PrefixOrigin,
SuffixOrigin, AddressState
}