Finished SetStaticRoute.ps1 script.

This commit is contained in:
Tanner van Teeffelen 2026-05-22 10:55:44 -04:00
parent 18b3e49270
commit 92a17335e8

View file

@ -22,26 +22,88 @@ Write-Host "|Developed by Tanner Van Teeffelen |" -ForegroundColor Blue
Write-Host "|Copyright ACO Services Inc. 2026 |" -ForegroundColor Blue Write-Host "|Copyright ACO Services Inc. 2026 |" -ForegroundColor Blue
Write-Host "-------------------------------------------`n" -ForegroundColor Blue Write-Host "-------------------------------------------`n" -ForegroundColor Blue
# 1 - Get the desired IP address. $inputValue = 0
$ipAddress = Read-Host "Which IP address do you want to staticly route?" $goodValue = "bad"
# 2 - Get the desired interface. while ($goodValue -ne "good"){
$interfaceChoiceValid = "false"
while ($interfaceChoiceValid -eq "false"){ $inputvalue = Read-Host "Press [1] to set a static route, press [2] to remove a static route, or [9] to cancel"
$interfaceChoice = Read-Host "Do you know the index of your desired VPN interface? (y/n)" Write-Host "`n"
if ($interfaceChoice.ToLower() -eq "y"){
$interfaceChoiceValid = "true" switch ($inputValue){
}
elseif ($interfaceChoice.ToLower() -eq "n"){ 1 {
$interfaceChoiceValid = "true" $goodValue = "good"
Write-Host "Showing all available interfaces..." # 1 - Get the desired IP address.
sleep 3 $ipAddress = Read-Host "Which IP address do you want to staticly route?"
route print
} # 2 - Get the desired interface.
else{ $interfaceChoiceValid = "false"
Write-Host "Please put in a valid choice."
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?"