diff --git a/SetStaticRoute.ps1 b/SetStaticRoute.ps1 index 208f76b..8229370 100644 --- a/SetStaticRoute.ps1 +++ b/SetStaticRoute.ps1 @@ -22,26 +22,88 @@ Write-Host "|Developed by Tanner Van Teeffelen |" -ForegroundColor Blue Write-Host "|Copyright ACO Services Inc. 2026 |" -ForegroundColor Blue Write-Host "-------------------------------------------`n" -ForegroundColor Blue -# 1 - Get the desired IP address. -$ipAddress = Read-Host "Which IP address do you want to staticly route?" +$inputValue = 0 +$goodValue = "bad" -# 2 - Get the desired interface. -$interfaceChoiceValid = "false" +while ($goodValue -ne "good"){ -while ($interfaceChoiceValid -eq "false"){ - $interfaceChoice = Read-Host "Do you know the index of your desired VPN interface? (y/n)" - if ($interfaceChoice.ToLower() -eq "y"){ - $interfaceChoiceValid = "true" - } - elseif ($interfaceChoice.ToLower() -eq "n"){ - $interfaceChoiceValid = "true" - Write-Host "Showing all available interfaces..." - sleep 3 - route print - } - else{ - Write-Host "Please put in a valid choice." + $inputvalue = Read-Host "Press [1] to set a static route, press [2] to remove a static route, or [9] to cancel" + Write-Host "`n" + + switch ($inputValue){ + + 1 { + $goodValue = "good" + # 1 - Get the desired IP address. + $ipAddress = Read-Host "Which IP address do you want to staticly route?" + + # 2 - Get the desired interface. + $interfaceChoiceValid = "false" + + while ($interfaceChoiceValid -eq "false"){ + $interfaceChoice = Read-Host "Do you know the index of your desired VPN interface? (y/n)" + if ($interfaceChoice.ToLower() -eq "y"){ + $interfaceChoiceValid = "true" + } + elseif ($interfaceChoice.ToLower() -eq "n"){ + $interfaceChoiceValid = "true" + Write-Host "Showing all available interfaces..." + Start-Sleep 1 + Get-NetIPInterface -AddressFamily IPv4 | Format-Table InterfaceIndex, InterfaceAlias, ConnectionState -AutoSize | Out-Host + } + else{ + Write-Host "Please select in a valid choice." + } + } + + $interfaceIndex = Read-Host "Which interface index do you want to staticly route through?" + + # 3 - Get the desired gateway. + $gatewayTemp = $gateway = (($ipAddress -split '\.')[0..2] -join '.') + '.1' + $gateway = Read-Host "Which gateway do you want to staticly route through? (leave blank for $gatewayTemp)" + + if ([string]::IsNullOrEmpty($gateway)){ + $gateway = $gatewayTemp + } + + #4. Set the static route. + Write-Host "Setting static route..." + Start-Sleep 1 + + try { + New-NetRoute -DestinationPrefix "$ipAddress/32" -InterfaceIndex $interfaceIndex -NextHop $gateway -ErrorAction Stop + Write-Host "`nRoute added successfully." -ForegroundColor Green + } + catch { + Write-Host "ERROR adding route: $($_.Exception.Message)" -ForegroundColor Red + } + } + + 2 { + $goodValue = "good" + # 1 - Get the desired IP address. + $ipAddress = Read-Host "Which IP address do you want to remove the static route for?" + + $prefix = "$ipAddress/32" + + # 2 - Remove the static route. + try { + Get-NetRoute -DestinationPrefix $prefix | Remove-NetRoute -ErrorAction Stop + Write-Host "`nRoute removed successfully." -ForegroundColor Green + } + catch { + Write-Host "ERROR removing route: $($_.Exception.Message)" -ForegroundColor Red + } + } + + 9 { + $goodValue = "good" + } + default { + Write-Host "Please choose a valid option." + } } } -$interfaceIndex = Read-Host "Which interface index do you want to staticly route through?" \ No newline at end of file + +