Last active
May 4, 2018 01:50
-
-
Save zailleh/201b0bd53c96bc75165eacc3356647de to your computer and use it in GitHub Desktop.
Script to be compiled into a Service that will automatically switch off Wi-Fi when an Ethernet connection is detected and switch it back on when no Ethernet connection is detected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#"Running" | Out-File "C:\Temp\Service.log" -append | |
Do { | |
#"Start Of Loop" | Out-File "C:\Temp\Service.log" -append | |
$Adaptors = Get-WmiObject -query "SELECT * FROM Win32_NetworkAdapter WHERE PhysicalAdapter = 1 AND NOT Description LIKE '%Virtual%' AND (NetConnectionID LIKE '%Wireless Network Connection%' OR NetConnectionID LIKE '%Local Area Connection%' OR NetConnectionID LIKE '%Wi-Fi%' OR NetConnectionID LIKE '%Ethernet%')" | |
$NetEnabledAdaptors = @($Adaptors | Where {$_.NetEnabled -eq $true}).Count | |
#"Adaptors Found: $($Adaptors.Count)" | Out-File "C:\Temp\Service.log" -append | |
#"Net Enabled Adaptors: $NetEnabledAdaptors" | Out-File "C:\Temp\Service.log" -append | |
if ($NetEnabledAdaptors -gt 1) | |
{ | |
#"WiFi and LAN Connected. Disabling WiFi..." | Out-File "C:\Temp\Service.log" -append | |
$WiFi = ($Adaptors | Where {($_.NetConnectionID -like "Wireless Network Connection*" -or $_.NetConnectionID -LIKE '*Wi-Fi*') -and $_.NetEnabled -eq $true}) | |
#"Disabling Adaptor... $($WiFi.Description)" | Out-File "C:\Temp\Service.log" -append | |
$returnvalue = $WiFi.Disable().ReturnValue | |
If ($returnvalue -eq 0) | |
{ | |
#"WiFi Disabled Successfully" | Out-File "C:\Temp\Service.log" -append | |
} | |
else | |
{ | |
#"Error Disabling WiFi: $returnvalue" | Out-File "C:\Temp\Service.log" -append | |
} | |
} | |
elseif ($NetEnabledAdaptors -eq 0) | |
{ | |
#"No connections detected. Enabling WiFi..." | Out-File "C:\Temp\Service.log" -append | |
$WiFi = ($Adaptors | Where {($_.NetConnectionID -like "Wireless Network Connection*" -or $_.NetConnectionID -LIKE '*Wi-Fi*') -and $_.NetEnabled -eq $false}) | |
#"Enabling Adaptor... $($WiFi.Description)" | Out-File "C:\Temp\Service.log" -append | |
$returnvalue = $WiFi.Enable().ReturnValue | |
If ($returnvalue -eq 0) | |
{ | |
#"WiFi Enabled Successfully" | Out-File "C:\Temp\Service.log" -append | |
} | |
else | |
{ | |
#"Error Enabling WiFi: $returnvalue" | Out-File "C:\Temp\Service.log" -append | |
} | |
} | |
else { | |
#"Only One Connection... No Action required." | Out-File "C:\Temp\Service.log" -append | |
} | |
#"Sleeping" | Out-File "C:\Temp\Service.log" -append | |
Start-Sleep -Seconds 5 | |
} while ($true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment