Created
November 6, 2020 13:58
-
-
Save machv/73e0d7c9907b1ad2d8c60eb69a5f3393 to your computer and use it in GitHub Desktop.
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 Get-RrasRoutes { | |
$config = netsh routing dump | |
$config | ? { $_.StartsWith("add persistentroute") } | ForEach-Object { | |
$line = $config | ? { $_.StartsWith("add persistentroute") } | Select -First 1 | |
$line = $_ | |
$fields = $line -split " " | |
$route = @{} | |
foreach($item in $fields) { | |
#$item = $fields | Select -First 1 -Skip 2 | |
$f = $item -split "=" | |
if($f.Length -eq 2) { | |
switch ($f[0]) { | |
"dest" { | |
$route["Prefix"] = $f[1] | |
} | |
"mask" { | |
$route["Mask"] = $f[1] | |
} | |
"name" { | |
$route["Interface"] = $f[1].Replace('"', "") | |
} | |
"nhop" { | |
$route["Via"] = $f[1] | |
} | |
} | |
} | |
} | |
[PSCustomObject]$route | |
} | |
} | |
$csvRoutes = Import-Csv -Delimiter ";" -Encoding UTF8 -Path .\routes.csv | |
$existingRoutes = Get-RrasRoutes | |
$dryRun = $false | |
$csvRoute = $csvRoutes | Select -First 1 -Skip 1 | |
foreach($csvRoute in $csvRoutes) { | |
$existing = $existingRoutes | Where-Object { $_.Prefix -eq $csvRoute.Prefix -and $_.Mask -eq $csvRoute.Mask } | |
if($existing) { | |
if($existing.Via -ne $csvRoute.Via -or $existing.Interface -ne $csvRoute.Interface) { | |
" [*] Update [dest=$($csvRoute.Prefix) mask=$($csvRoute.Mask) name=`"$($existing.Interface) -> $($csvRoute.Interface)`" nhop=$($existing.Via) -> $($csvRoute.Via)]" | |
if(-not $dryRun) { | |
$result = & netsh routing ip delete persistentroute dest=$($existing.Prefix) mask=$($existing.Mask) name=`"$($existing.Interface)`" nhop=$($existing.Via) | |
if(($result | Select -First 1) -ne "Ok.") { | |
$result | |
} | |
$result = & netsh routing ip add persistentroute dest=$($csvRoute.Prefix) mask=$($csvRoute.Mask) name=`"$($csvRoute.Interface)`" nhop=$($csvRoute.Via) | |
if(($result | Select -First 1) -ne "Ok.") { | |
$result | |
} | |
} | |
} else { | |
" [ ] OK [dest=$($csvRoute.Prefix) mask=$($csvRoute.Mask) name=`"$($csvRoute.Interface)`" nhop=$($csvRoute.Via)]" | |
} | |
} | |
else { | |
" [+] Add [dest=$($csvRoute.Prefix) mask=$($csvRoute.Mask) name=`"$($csvRoute.Interface)`" nhop=$($csvRoute.Via)]" | |
if(-not $dryRun) { | |
$result = & netsh routing ip add persistentroute dest=$($csvRoute.Prefix) mask=$($csvRoute.Mask) name=`"$($csvRoute.Interface)`" nhop=$($csvRoute.Via) | |
if(($result | Select -First 1) -ne "Ok.") { | |
$result | |
} | |
} | |
} | |
} | |
$updatedRoutes = Get-RrasRoutes | |
$updatedRoutes | ForEach-Object { | |
$rrasRoute = $_ | |
$csvRoute = $csvRoutes | Where-Object { $_.Prefix -eq $rrasRoute.Prefix -and $_.Mask -eq $rrasRoute.Mask -and $_.Interface -eq $rrasRoute.Interface -and $_.Via -eq $rrasRoute.Via } | |
if(-not $csvRoute) { | |
" [-] Remove [dest=$($rrasRoute.Prefix) mask=$($rrasRoute.Mask) name=`"$($rrasRoute.Interface)`" nhop=$($rrasRoute.Via)]" | |
if(-not $dryRun) { | |
$result = & netsh routing ip delete persistentroute dest=$($rrasRoute.Prefix) mask=$($rrasRoute.Mask) name=`"$($rrasRoute.Interface)`" nhop=$($rrasRoute.Via) | |
if(($result | Select -First 1) -ne "Ok.") { | |
$result | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment