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,6 +22,18 @@ 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?"
@ -36,12 +48,62 @@ while ($interfaceChoiceValid -eq "false"){
elseif ($interfaceChoice.ToLower() -eq "n"){
$interfaceChoiceValid = "true"
Write-Host "Showing all available interfaces..."
sleep 3
route print
Start-Sleep 1
Get-NetIPInterface -AddressFamily IPv4 | Format-Table InterfaceIndex, InterfaceAlias, ConnectionState -AutoSize | Out-Host
}
else{
Write-Host "Please put in a valid choice."
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."
}
}
}