-
Notifications
You must be signed in to change notification settings - Fork 1
/
routeadd.ps1
52 lines (49 loc) · 1.88 KB
/
routeadd.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
if ((Get-Host).Version.Major -lt 7) {
echo "you must install power shell 7 before use this script!"
exit -1;
}
$powershellexe=[System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName
$routenb=(Get-NetRoute | Where-Object {$_.DestinationPrefix -eq "10.10.10.0/24" } | measure).Count
$scriptpath= $MyInvocation.MyCommand.Source, $args | %{ $_ }
if ($routenb -eq 0) {
# require admin elevation
if (!
#current role
(New-Object Security.Principal.WindowsPrincipal(
[Security.Principal.WindowsIdentity]::GetCurrent()
#is admin?
)).IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator
)
) {
#elevate script and exit current non-elevated runtime
echo "Route add need admin privileg!"
echo 'Run the script in admin to add route...'
Start-Process -Verb RunAs -WindowStyle Hidden -FilePath $powershellexe -ArgumentList ( '-File', $scriptpath )
$iteration=0
while(($routenb=(Get-NetRoute | Where-Object {$_.DestinationPrefix -eq "10.10.10.0/24" } | measure).Count) -eq 0)
{
Start-Sleep -Milliseconds 100
$iteration++
if ($routenb -ne 0 -or $iteration -gt 20) {
break
}
}
if ($routenb -ne 0) {
echo "Route added!"
Get-NetRoute | Where-Object {$_.DestinationPrefix -eq "10.10.10.0/24" }
} else {
echo "Error: route not added!"
echo "use this command in admin powershell: route add 10.10.10.0/24 172.22.100.2"
echo "to add manualy the route"
}
exit
}
else {
$ifindex=(Get-NetIPAddress | Where-Object {$_.IPv4Address -eq "172.22.100.1" }).ifIndex
#echo "add route for 10.10.10.0/24 on interface index: $ifindex"
New-NetRoute -DestinationPrefix "10.10.10.0/24" -InterfaceIndex $ifindex -NextHop "172.22.100.2" -PolicyStore "ActiveStore"
}
} else {
echo "Route already added"
}