Created
April 11, 2022 09:49
-
-
Save jwhb/57e20b430b78317a66855870eba3f01e to your computer and use it in GitHub Desktop.
Windows Route Backup (useful for Windows Container usage like RKE2 where HNS configuration wipes the routes)
This file contains 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
# Route Backup | |
# | |
# Creates a script at C:\eth_routes.ps1 (or user Desktop) with `route add` statements to re-apply routing table entries of a specific interface. | |
# | |
# Useful for Windows Container usage like RKE and RKE2 Kubernetes, where HNS configuration wipes parts of the routing table. | |
# Related issue: https://projectcalico.docs.tigera.io/getting-started/windows-calico/troubleshoot#after-initializing-calico-for-windows-aws-metadata-server-is-no-longer-reachable | |
$ifname = "Ethernet" | |
$default_metric = 5 | |
# determine if user has administrative privilege | |
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) | |
$is_admin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | |
$route_cmd_file = if ($is_admin) { | |
"C:\eth_routes.ps1" | |
} else { | |
$desktop_path = [Environment]::GetFolderPath("Desktop") | |
Join-Path -Path $desktop_path -ChildPath "eth_routes.ps1" | |
} | |
$eth_ifindex = Get-NetAdapter -Name $ifname | select ifIndex -ExpandProperty ifIndex | select -first 1 | |
$commands = @() | |
Get-NetRoute -InterfaceIndex $eth_ifindex | ForEach-Object { | |
$metric = if ($_.RouteMetric -ne 0) { $_.RouteMetric } else { $default_metric } # set default metric value if Get-NetAdapter returned metric 0 | |
$commands += "ROUTE -P ADD $($_.DestinationPrefix) $($_.NextHop) METRIC $($metric)" | |
} | |
$commands -join "`n" | Out-File -FilePath $route_cmd_file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment