Last active
November 7, 2024 01:38
-
-
Save changbowen/c6341bfafc967a1a4aab41ca1d290d84 to your computer and use it in GitHub Desktop.
PowerShell script that monitors for IP changes and updates route metric according to internet reachability
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
function ipChangeHandler { | |
Write-Host "Detected an IP change..." | |
Write-Host "Collecting connections information..." | |
$connObjs = New-Object System.Collections.ArrayList | |
$ints = Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Dhcp -AddressState Preferred | |
foreach ($int in $ints) { | |
$_alias = $int.InterfaceAlias | |
$_index = $int.InterfaceIndex | |
$_ipaddr = $int.IPAddress | |
$intCfg = $int | Get-NetIPConfiguration | |
$_gateway = $intCfg.IPv4DefaultGateway.NextHop | |
$intAdp = Get-WmiObject -Class Win32_NetworkAdapter | where InterfaceIndex -EQ $_index | select InterfaceIndex, Speed | |
$_linkspeed = $intAdp.Speed / 1000000 | |
$_free = "" -ne ((ping -n 1 -S $_ipaddr 8.8.8.8) -like "*Received = 1*") | |
$connObjs.Add([PSCustomObject]@{ | |
InterfaceAlias = $_alias; | |
IPAddress = $_ipaddr; | |
Gateway = $_gateway; | |
LinkSpeedMB = $_linkspeed; | |
Free = $_free; | |
}) > $null | |
} | |
if (0 -eq @($connObjs | where Free -EQ $true).Count) { | |
Write-Host "No internet connnection detected. Resetting default gateway metric..." | |
Set-NetRoute -DestinationPrefix 0.0.0.0/0 -RouteMetric 0 -Confirm:$false | |
} | |
else { | |
Write-Host "Setting all default gateways' metric to max..." | |
Set-NetRoute -DestinationPrefix 0.0.0.0/0 -RouteMetric 10000 -Confirm:$false | |
$connObjs | where Free -EQ $true | foreach { | |
Write-Host ("Setting route metric on " + $_.Gateway + "...") | |
Get-NetRoute -DestinationPrefix 0.0.0.0/0 -NextHop $_.Gateway -InterfaceAlias $_.InterfaceAlias | Set-NetRoute -RouteMetric 0 | |
} | |
Write-Host "Cycle completed." | |
} | |
$connObjs = $null; | |
$ints = $null; | |
Write-Host; | |
} | |
function cleanUp { | |
Write-Host "Unregistering events..." | |
Unregister-Event -SourceIdentifier ADGWM_Timer -ErrorAction Ignore; | |
Unregister-Event -SourceIdentifier ADGWM_IPChange -ErrorAction Ignore; | |
} | |
###################### execution starts here ###################### | |
cleanUp; | |
$timer = New-Object System.Timers.Timer -Property @{ Interval=5000; AutoReset=$false }; | |
Register-ObjectEvent $timer -EventName Elapsed -SourceIdentifier ADGWM_Timer -Action { ipChangeHandler } > $null; | |
Write-Host "Registering for IP change event..." | |
$networkChange = [System.Net.NetworkInformation.NetworkChange]; | |
Register-ObjectEvent -InputObject $networkChange -EventName NetworkAddressChanged -SourceIdentifier ADGWM_IPChange -Action { | |
$timer.Stop(); | |
$timer.Start(); | |
} > $null; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment