-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetRouteToHotspot.ps1
78 lines (63 loc) · 2.57 KB
/
SetRouteToHotspot.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
# SetRouteToHotspot
# Set a Route using shared network by phone hotspot
# Variable for phone network adapter and DCHP IP range, modify to use of other phone
# phone adapter name ,String , can use wildcard(*) to filter, e.g: *SAMSUNG*
# use "Get-NetAdapter" command and use string in InterdaceDescription
$AdapterName = "SAMSUNG*"
# RouteToUseSharedNetwork: String, use IPV4/mask, used for add route
$RouteToUseSharedNetwork = "172.20.0.0/16"
# SharedNetworkGetway: String, can use wildcard(*), e.g: 192.168.*, used for setting nexthop
$SharedNetworkGetway = "192.168.*"
# Start of program
Write-Output "Remove old or deprecated route for $RouteToUseSharedNetwork..."
Write-Output ""
$oldRouteExist = Get-NetRoute | Select-Object DestinationPrefix | Select-String $RouteToUseSharedNetwork
if ( $oldRouteExist ){
try {
Write-Output "Now Remove the route: $oldRouteExist"
Write-Output ""
Remove-NetRoute -DestinationPrefix $RouteToUseSharedNetwork -Confirm:$False
}
catch {
Write-Output "Something threw an exception"
Write-Output $_
Read-Host -Prompt "Press Enter to exit"
return
}
}
# Get Samsung network interface information
Write-Output "Getting Information of adapter: $AdapterName..."
Write-Output ""
try {
$CheckAdapter = Get-NetAdapter | Select-Object InterfaceDescription | Select-String $AdapterName
if (!$CheckAdapter) {
Write-Output "Cannot find out adapter interface information:$AdapterName. Not plug in or network not shared?"
Write-Output "Check network sharing configuration on device and try again"
Read-Host -Prompt "Press Enter to exit"
return
}
$ifIndex = Get-NetAdapter -InterfaceDescription $AdapterName | Select-Object -ExpandProperty 'ifIndex'
$phoneRouteGetway = Get-NetRoute -ifIndex $ifIndex -NextHop $SharedNetworkGetway | Select-Object -ExpandProperty "Nexthop"
}
catch {
Write-Output "Something threw an exception"
Write-Output $_
Read-Host -Prompt "Press Enter to exit"
return
}
Write-Output "interface index number of adapter:$AdapterName : $ifIndex\n"
Write-Output "getway: $phoneRouteGetway"
Write-Output ""
# Set new route and change metric, dest:0.0.0.0/0 use new route with low priority
try {
New-NetRoute -DestinationPrefix $RouteToUseSharedNetwork -NextHop $phoneRouteGetway -InterfaceIndex $ifIndex -RouteMetric 25
Set-NetRoute -DestinationPrefix "0.0.0.0/0" -NextHop $phoneRouteGetway -InterfaceIndex $ifIndex -RouteMetric 100
}
catch {
<#Do this if a terminating exception happens#>
Write-Output "Something threw an exception"
Write-Output $_
Read-Host -Prompt "Press Enter to exit"
return
}
Read-Host -Prompt "Press Enter to exit"