-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathnet-route-add.ps1
221 lines (181 loc) · 6.96 KB
/
net-route-add.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<#
.SYNOPSIS
This script is used to add a network route to the routing table.
.DESCRIPTION
The script allows the user to add a new route to the routing table by specifying the destination network, subnet mask, gateway, and optionally the interface and metric.
.PARAMETER Destination
The destination network address for the route.
.PARAMETER SubnetMask
The subnet mask for the destination network.
.PARAMETER Gateway
The gateway address for the route.
.PARAMETER Interface
(Optional) The network interface to use for the route.
.PARAMETER Metric
(Optional) The metric for the route.
.EXAMPLE
.\net-route-add.ps1 -Destination "192.168.1.0" -SubnetMask "255.255.255.0" -Gateway "192.168.1.1"
This example adds a route to the 192.168.1.0/24 network via the gateway 192.168.1.1.
.EXAMPLE
.\net-route-add.ps1 -Destination "10.0.0.0" -SubnetMask "255.0.0.0" -Gateway "10.0.0.1" -Interface 2 -Metric 10
This example adds a route to the 10.0.0.0/8 network via the gateway 10.0.0.1 using interface 2 with a metric of 10.
.NOTES
Author: jagilber
Date: 240924
Version: 1.0
#>
param(
[string]$destinationPrefix, # ip cidr Ex: 8.8.8.8/32
[string]$interfaceAlias, # 'Ethernet', 'Wi-Fi', etc.
[int]$interfaceIndex, # 1, 2, 3, etc.
[ValidateSet("IPv4", "IPv6")]
[string]$addressFamily = "IPv4",
[int]$routeMetric = 0, # 0, 1, 2, etc.
[string]$nextHop = $null, # gateway ip address
[switch]$force,
[switch]$remove,
[string]$cidr = "/32",
[int]$lifetimeHours = 8
)
function main() {
if (!(is-admin)) {
return
}
write-host "network interfaces:" -ForegroundColor Green
write-host "get-netIpInterface"
get-netIpInterface | out-string
write-host "network ip configurations:" -ForegroundColor Green
write-host "get-netIPConfiguration"
$ipConfigurations = get-netIPConfiguration
$ipConfigurations | out-string
$global:ipConfigurations = $ipConfigurations
write-host "get-netAdapter | where-object status -eq 'Up'"
$activeAdapters = @(get-netAdapter | where-object {$psitem.status -eq 'Up' -and $psitem.PhysicalMediaType -ine 'unspecified'})
$activeAdapters = $activeAdapters | sort-object -Property InterfaceIndex
$primaryAdapter = $activeAdapters[0]
$global:primaryAdapter = $primaryAdapter
write-host "active adapters:" -ForegroundColor Green
$activeAdapters | out-string
if (!$activeAdapters) {
Write-Error "No active adapters found."
return
}
write-host "active vpn connections:" -ForegroundColor Green
write-host "Get-VpnConnection"
$vpnConnections = Get-VpnConnection
$connectedVpnConnections = $vpnConnections | where-object ConnectionStatus -eq 'Connected'
$connectedVpnConnections | Format-List *
if ($interfaceIndex -and $interfaceAlias) {
Write-Error "You must specify either an interface index or an interface alias, not both."
return
}
if (!$interfaceIndex -and !$interfaceAlias) {
$interfaceIndex = $primaryAdapter.InterfaceIndex
write-warning "No interface specified. Using default interface index $($interfaceIndex)."
$global:defaultGatewayConfiguration = ($ipConfigurations | where-object InterfaceIndex -eq $interfaceIndex).IPv4DefaultGateway
$nextHop = $global:defaultGatewayConfiguration.NextHop
if(!$nextHop){
Write-Error "No default gateway found for interface index $($interfaceIndex)."
return
}
write-warning "No next hop specified. Using default gateway '$($nextHop)'."
$routeMetric = $global:defaultGatewayConfiguration.RouteMetric
}
if (!$nextHop -and !$force) {
Write-Error "You must specify a next hop address."
return
}
try {
$destinationPrefix = $destinationPrefix.Trim().Replace("'", '').Replace('"', '')
$null = [System.Net.IPAddress]::Parse($destinationPrefix.Split('/')[0])
$destinationPrefixes = @($destinationPrefix)
}
catch {
if (!$destinationPrefix) {
Write-Warning "No destination prefix specified."
return
}
write-warning "Invalid destination prefix. attempting to resolve..."
if($destinationPrefix -imatch '^(?:https?://)?(?<fqdn>[^/]+)'){
$destinationPrefix = $matches['fqdn']
write-host "destinationPrefix: $destinationPrefix"
}
write-host "Resolve-DnsName -Name $destinationPrefix -Type A"
$destinationPrefixes = @(Resolve-DnsName -Name $destinationPrefix -Type A).IPAddress
if ($destinationPrefixes.Count -eq 0) {
Write-Warning "Could not resolve destination prefix."
return
}
else {
$error.Clear()
}
}
foreach ($destinationPrefix in $destinationPrefixes) {
$prefixParams = @{
DestinationPrefix = "$($destinationPrefix)$($cidr)"
AddressFamily = $addressFamily
RouteMetric = $routeMetric
NextHop = $nextHop
}
if ($interfaceAlias) {
$prefixParams.InterfaceAlias = $interfaceAlias
}
else {
$prefixParams.InterfaceIndex = $interfaceIndex
}
check-route -prefixParams $prefixParams
}
}
function check-route($prefixParams) {
$prefix = $prefixParams.DestinationPrefix
write-host "checking route for $prefix"
write-host "get-netRoute $($prefixParams | convertto-json)"
$route = get-netRoute @prefixParams -ErrorAction SilentlyContinue
if ($route) {
if ($force -or $remove) {
write-host "found and removing route..."
write-host "remove-netRoute $($prefixParams | convertto-json)"
remove-netRoute @prefixParams -confirm:$force
return
}
Write-Warning "Route already exists."
return
}
if (!$route -and $remove) {
Write-Warning "Route does not exist."
return
}
if ($lifetimeHours) {
# $timespan = (Get-Date).AddHours($lifetimeHours) - (Get-Date)
#$timespan = new-timespan -Start (Get-Date) -End (Get-Date).AddHours($lifetimeHours)
$prefixParams.PolicyStore = 'ActiveStore' # by default it is added to 'PersistentStore' and will not be removed after reboot
# not working
$prefixParams.ValidLifetime = (new-timespan -hours $lifetimeHours) # [timeSpan]::FromHours($lifetimeHours)
$prefixParams.PreferredLifetime = (new-timespan -hours $lifetimeHours) #[timeSpan]::FromHours($lifetimeHours)
}
write-host "new-netRoute $($prefixParams | convertto-json)"
$error.Clear()
$result = new-netRoute @prefixParams
if ($error.Count -gt 0) {
Write-Warning "Error adding route."
$result
return
}
write-host "route added."
}
function is-admin() {
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( `
[Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning "please restart script as administrator. exiting..."
$command = 'pwsh'
$commandLine = $global:myinvocation.myCommand.definition
if ($psedition -eq 'Desktop') {
$command = 'powershell'
}
write-host "Start-Process $command -Verb RunAs -ArgumentList `"-NoExit -File $commandLine`""
Start-Process $command -Verb RunAs -ArgumentList "-NoExit -File $commandLine"
return $false
}
return $true
}
main