NetworkUtilities/SetStaticRoute.ps1
2026-05-22 10:55:44 -04:00

109 lines
3.9 KiB
PowerShell

<#
NetworkUtilities - SetStaticRoute
Copyright (c) 2026 ACO Services Inc.
Developed by Tanner Van Teeffelen
#>
Clear-Host
Add-Type -AssemblyName System.Windows.Forms
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(` [Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Host "Insufficient permissions to run this script. Please run this as an administrator.`n" -ForegroundColor Red
$HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL
$HOST.UI.RawUI.Flushinputbuffer()
exit 1
}
Write-Host "-------------------------------------------" -ForegroundColor Blue
Write-Host "|NetworkUtilities - SetStaticRoute |" -ForegroundColor Blue
Write-Host "|Developed by Tanner Van Teeffelen |" -ForegroundColor Blue
Write-Host "|Copyright ACO Services Inc. 2026 |" -ForegroundColor Blue
Write-Host "-------------------------------------------`n" -ForegroundColor Blue
$inputValue = 0
$goodValue = "bad"
while ($goodValue -ne "good"){
$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."
}
}
}