-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSwitch-NetAdapter.ps1
76 lines (73 loc) · 3 KB
/
Switch-NetAdapter.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
$ConfirmPreference = "none"
$wifiNet = "Wi-Fi"
$cableNet = "Ethernet"
Write-Host "Running Script to switch Net-Adapter" -ForegroundColor DarkCyan
Write-Host "Switch between $wifiNet - $cableNet "
$wifiEnabled = $false
$cableEnabled = $false
trap {
Write-Host "Erro Found:`n$_" -ForegroundColor Red
Read-Host "Press any key to continue"
}
foreach ($adapter in (Get-NetAdapter))
{
if($adapter.Name -eq $wifiNet)
{
if($adapter.Status -ne "Disabled"){
$wifiEnabled = $true
continue
}
Write-Host "Enabling:$wifiNet " -NoNewline
Write-Host "Disabling:$cableNet " -NoNewline
Disable-NetAdapter -Name $cableNet
Enable-NetAdapter -Name $wifiNet
Write-Host "Disabling:Proxy"
Set-Itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyEnable -value 0
#O Windows has to open IE to update proxy settings
$ie = new-object -ComObject internetexplorer.application
$ie.Quit()
Write-Host "$wifiNet Enabled and " -ForegroundColor Green -NoNewline
Write-Host "Proxy Disabled" -ForegroundColor Red
$wifiEnabled = $true
$cableEnabled = $false
break
}
elseif($adapter.Name -eq $cableNet)
{
if($adapter.Status -ne "Disabled"){
$cableEnabled = $true
continue
}
Write-Host "Enabling:$cableNet " -NoNewline
Write-Host "Disabling:$wifiNet " -NoNewline
Disable-NetAdapter -Name $wifiNet
Enable-NetAdapter -Name $cableNet
Write-Host "Enabling:Proxy"
set-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyEnable -value 1
#O Windows has to open IE to update proxy settings
$ie = new-object -ComObject internetexplorer.application
$ie.Quit()
Write-Host "$cableNet Enabled and " -ForegroundColor Green -NoNewline
Write-Host "Proxy Enabled" -ForegroundColor Green
$cableEnabled = $true
$wifiEnabled = $false
break
}
}
if($wifiEnabled -and $cableEnabled){
Write-Host "Warning: Both are Enabled, so disable $cableNet" -ForegroundColor Yellow
Write-Host "Enabling:$wifiNet " -ForegroundColor Green -NoNewline
Write-Host "Disabling:$cableNet " -ForegroundColor Red -NoNewline
Disable-NetAdapter -Name $cableNet
Enable-NetAdapter -Name $wifiNet
Write-Host "Disabling:Proxy" -ForegroundColor Red
Set-Itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyEnable -value 0
#O Windows has to open IE to update proxy settings
$ie = new-object -ComObject internetexplorer.application
$ie.Quit()
Write-Host "$wifiNet Enabled and " -ForegroundColor Green -NoNewline
Write-Host "Proxy Disabled" -ForegroundColor Red
}elseif((-not $wifiEnabled) -and (-not $cableEnabled)){
Write-Host "Erro: $wifiNet and $cableNet does not Exist. Set then properly" -ForegroundColor Red
}
Read-Host "Press any key to continue"