-
Notifications
You must be signed in to change notification settings - Fork 0
/
OVPN_chg190118_v6.ps1
66 lines (63 loc) · 2.21 KB
/
OVPN_chg190118_v6.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
[CmdletBinding()]
param (
[switch] $Disconnect,
[switch] $Connect,
[switch] $Info,
[switch] $Load
)
function __Openvpn_NetAdapter__ {
return (Get-NetAdapter -InterfaceDescription "TAP-Windows Adapter V9")
}
function __Openvpn_Status__ {
return ($(__Openvpn_NetAdapter__).Status -eq "Up")
}
function OVPN_chg190118_v6 {
param (
[switch] $Disconnect,
[switch] $Info
)
if ($Info) {
$ms_tcpip6 = (Get-NetAdapterBinding -Com:ms_tcpip6) | Select-Object Name, @{Name='IPv6'; Expression='Enabled'}
$ms_tcpip4 = (Get-NetAdapterBinding -Com:ms_tcpip) | Select-Object Name, @{Name='IPv4'; Expression='Enabled'}
$ms_tcpip4 | LeftJoin $ms_tcpip6 -On Name | Out-Default
} else {
$ovpn = "C:\Program Files\OpenVPN\bin\openvpn-gui.exe"
$ovpnSetting = "chg190118_v6.ovpn"
if ($Disconnect) {
.$ovpn --command disconnect $ovpnSetting
Get-NetAdapterBinding -ComponentID:ms_tcpip6 | Where-Object{!($_.Enabled)} | ForEach-Object{Enable-NetAdapterBinding -Name:$_.Name -ComponentID:ms_tcpip6}
while (__Openvpn_Status__) {
Write-Host "openVPN DisConnecting..."
Start-Sleep -s 1
}
} else {
$job = Start-Job { ((Get-NetConnectionProfile).InterfaceAlias | ForEach-Object{ Disable-NetAdapterBinding -ComponentID:ms_tcpip6 -Name:$_ }) }
Wait-Job $job | Out-Null; Receive-Job $job
if (!(__Openvpn_Status__)){
.$ovpn --connect $ovpnSetting
while (!(__Openvpn_Status__)) {
Write-Host "openVPN connecting..."
Start-Sleep -s 1
}
}
}
}
$(__Openvpn_NetAdapter__) | Select-Object Name, InterfaceDescription, Status
}
function OVPN_chg190118_v6_Switch {
if (__Openvpn_Status__) { OVPN_chg190118_v6 -Disconnect }
else { OVPN_chg190118_v6 }
}
function __main__ {
if ($Load) {
return
} elseif ($Connect) {
OVPN_chg190118_v6
} elseif ($Disconnect) {
OVPN_chg190118_v6 -Disconnect
} elseif ($Info) {
OVPN_chg190118_v6 -Info:$Info
} else {
OVPN_chg190118_v6_Switch
}
} __main__