This repository has been archived by the owner on May 28, 2020. It is now read-only.
forked from richardhicks/aovpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemove-VpnConnections.ps1
129 lines (110 loc) · 6.35 KB
/
Remove-VpnConnections.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
<#
.SYNOPSIS
PowerShell script to remove stale VPN connections.
.PARAMETER MaxAge
The value in seconds to remove VPN connections older than. The minimum accepted value is 3600 (1 hour). The default is 86400 (24 hours).
.EXAMPLE
.\Remove-VpnConnections -MaxAge 28800
.DESCRIPTION
It is not uncommon for Windows Server Routing and Remote Access Service (RRAS) servers to maintain VPN connections long after the user has disconnected. This PowerShell script can be used to remove those stale connections.
.LINK
https://directaccess.richardhicks.com/
.NOTES
Version: 1.0
Creation Date: April 14, 2020
Last Updated: April 14, 2020
Author: Richard Hicks
Organization: Richard M. Hicks Consulting, Inc.
Contact: [email protected]
Web Site: https://directaccess.richardhicks.com/
#>
[CmdletBinding()]
Param(
[ValidateRange(3600,86400)]
[string]$MaxAge = '86400'
)
$Connections = Get-RemoteAccessConnectionStatistics | Where-Object ConnectionDuration -ge $MaxAge | Select-Object -ExpandProperty UserName -Unique
# // If no connections exceed the value of MaxAge exit script
If ($null -eq $Connections) {
Write-Warning "No connections exceeding $MaxAge seconds. Exiting script."
Exit
}
# // Remove users with connections exceeding the value of MaxAge
Foreach ($User in $Connections) {
Write-Verbose "Removing VPN connections older than $MaxAge seconds..."
Disconnect-VpnUser -UserName $User
}
# SIG # Begin signature block
# MIINbAYJKoZIhvcN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