This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Start-AOVPN
72 lines (61 loc) · 2.83 KB
/
Start-AOVPN
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
Function Start-AOVPN {
<#
.Synopsis
Attempts to reconnect to AOVPN if the connection fails unexpectedly.
.DESCRIPTION
Use this script with a scheduled task that is triggered by Event ID's such as waking from sleep \ hibernation \ network change etc.
- Firstly a check is made to see if the computer is on the corporate domain, if it is, then do nothing.
- Then, if the computer is not on the corporate domain and all the while that the AOVPN is not connected, attempt up to x times to reconnect before (optionally) giving up.
.PARAMETER AOVPNName
The name of your AOVPN implementation. You can obtain this by running the following PowerShell cmdlet: (get-vpnconnection).name
.PARAMETER ConnectionAttempts
The number of reconnection attempts (1 to 60) to make following a 2 second pause until giving up. Enter 0 to try until a connection is made. Defaults to 30 attempts if you do not use this parameter.
.EXAMPLE
Start-AOVPN -AOVPNName "my-aovpn"
Attempt to reconnect to the AOVPN and try 30 times with 2 second intervals between each attempt. After 30 attempts, if the connection has not been made, then give up.
.EXAMPLE
Start-AOVPN -AOVPNName "my-aovpn" -ConnectionAttempts 10
Attempt to reconnect to the AOVPN and try 10 times with 2 second intervals between each attempt. After 10 attempts, if the connection has not been made, then give up.
.EXAMPLE
Start-AOVPN -AOVPNName "my-aovpn" -ConnectionAttempts 0
Attempt to reconnect to the AOVPN and try indefinitely with 2 second intervals between each attempt until a connection has been re-established.
.NOTES
Version: 1.0.0
Date: 20190628
Created By: OzThe2
.LINK
https://fearthepanda.com
.LINK
https://leanpub.com/configmgr-DeployUsingPS
#>
[CmdletBinding()]
param(
[Parameter(
Position = 0,
Mandatory = $true)]
[string][ValidateNotNullOrEmpty()]$AOVPNName,
[Parameter(
Position = 1,
Mandatory = $false)]
[Int32][ValidateRange(0,60)]$ConnectionAttempts = 30
)
BEGIN {
$Tries = 0
# -- Determine if we are domain authenticated or not --
Get-NetConnectionProfile | % {if ($_.NetworkCategory -eq 'DomainAuthenticated') {$OnDomain = $true;break}else{$OnDomain = $false}}
}
PROCESS {
if (!($OnDomain)) {
while ((Get-VpnConnection $AOVPNName).ConnectionStatus -ne 'Connected') {
# -- Attempt to reconnect --
start-process -FilePath $env:windir\system32\rasdial.exe -ArgumentList $AOVPNName
start-sleep -Seconds 2
if ($ConnectionAttempts -gt 0) {
$tries++
# -- Attempted to reconnect xx times but failed - give up --
if ($tries -eq $ConnectionAttempts) {exit}
}
}
}
}
}