Skip to content

Instantly share code, notes, and snippets.

@changbowen
Last active June 24, 2019 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save changbowen/03620154fc758005fc4732107faf71f6 to your computer and use it in GitHub Desktop.
Save changbowen/03620154fc758005fc4732107faf71f6 to your computer and use it in GitHub Desktop.
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 default gateway 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 1
}
Write-Host "Cycle completed."
}
$connObjs = $null;
$ints = $null;
Write-Host;
}
Unregister-Event -SourceIdentifier ADGWM_Timer -ErrorAction Ignore;
$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..."
Unregister-Event -SourceIdentifier ADGWM_IPChange -ErrorAction Ignore;
$networkChange = [System.Net.NetworkInformation.NetworkChange];
Register-ObjectEvent -InputObject $networkChange -EventName NetworkAddressChanged -SourceIdentifier ADGWM_IPChange -Action {
$timer.Stop();
$timer.Start();
} > $null;
<#
#check if no default gateway is present
Write-Host "Checking if no default gateway is present..."
if (0 -eq @(Get-NetRoute -DestinationPrefix 0.0.0.0/0 -ErrorAction Ignore).Count) {
$theOne = $connObjs | sort LinkSpeedMB -Descending | select -First 1
New-NetRoute -DestinationPrefix 0.0.0.0/0 -NextHop $theOne.Gateway -InterfaceAlias $theOne.InterfaceAlias -PolicyStore ActiveStore > $null
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment