-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDisconnect-all-current-P2S-VPN-connections.ps1
141 lines (99 loc) · 7 KB
/
Disconnect-all-current-P2S-VPN-connections.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
130
131
132
133
134
135
136
137
138
139
140
141
<#
.SYNOPSIS
A script used to disconnect all current P2S VPN connections.
.DESCRIPTION
A script used to disconnect all current P2S VPN connections.
The script will do all of the following:
Check if the PowerShell window is running as Administrator (when not running from Cloud Shell), otherwise the Azure PowerShell script will be exited.
Suppress breaking change warning messages.
Check Virtual Network Gateway parameter input. If the input is incorrect, the script will be exited.
Retrieve all current sessions and save them in a variable.
Disconnect all current sessions.
.NOTES
Filename: Disconnect-all-current-P2S-VPN-connections.ps1
Created: 19/08/2022
Last modified: 19/08/2022
Author: Wim Matthyssen
Version: 1.0
PowerShell: Azure PowerShell and Azure Cloud Shell
Requires: PowerShell Az (v5.9.0) and Az.Network (v4.16.0)
Action: Change variables were needed to fit your needs
Disclaimer: This script is provided "As Is" with no warranties.
.EXAMPLE
Connect-AzAccount
Get-AzTenant (if not using the default tenant)
Set-AzContext -tenantID "<xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx>" (if not using the default tenant)
Set-AzContext -Subscription "<SubscriptionName>" (if not using the default subscription)
.\Disconnect-all-current-P2S-VPN-connections.ps1 <"your virtual network gateway name here"> <"your virtual network gateway resource group name here">
.LINK
#>
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Parameters
param(
[parameter(Mandatory =$true)][ValidateNotNullOrEmpty()] [string] $gatewayName,
[parameter(Mandatory =$true)][ValidateNotNullOrEmpty()] [string] $rgNameGateway
)
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Variables
$global:currenttime= Set-PSBreakpoint -Variable currenttime -Mode Read -Action {$global:currenttime= Get-Date -UFormat "%A %m/%d/%Y %R"}
$foregroundColor1 = "Red"
$foregroundColor2 = "Yellow"
$writeEmptyLine = "`n"
$writeSeperatorSpaces = " - "
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Check if PowerShell runs as Administrator (when not running from Cloud Shell), otherwise exit the script
if ($PSVersionTable.Platform -eq "Unix") {
Write-Host ($writeEmptyLine + "# Running in Cloud Shell" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## Start script execution
Write-Host ($writeEmptyLine + "# Script started. Without any errors, it will need around 1 minute to complete" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
} else {
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdministrator = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
## Check if running as Administrator, otherwise exit the script
if ($isAdministrator -eq $false) {
Write-Host ($writeEmptyLine + "# Please run PowerShell as Administrator" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
Start-Sleep -s 3
exit
}
else {
## If running as Administrator, start script execution
Write-Host ($writeEmptyLine + "# Script started. Without any errors, it will need around 1 minute to complete" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
}
}
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Suppress breaking change warning messages
Set-Item Env:\SuppressAzurePowerShellBreakingChangeWarnings "true"
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Check Virtual Network Gateway parameter input. If the input is incorrect, the script will be exited
try {
Get-AzVirtualNetworkGateway -Name $gatewayName -ResourceGroupName $rgNameGateway -ErrorAction Stop | Out-Null
} catch {
Write-Host ($writeEmptyLine + "# VPN Gateway $gatewayName does not exist, please validate your input. The script will be exited" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
Start-Sleep -s 3
exit
}
Write-Host ($writeEmptyLine + "# Virtual Network Gateway with name $gatewayName exists in the current subscription. The script will continue" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Retrieve all current sessions and save them in a variable
$currentSessions = Get-AzVirtualNetworkGatewayVpnClientConnectionHealth -VirtualNetworkGatewayName $gatewayName -ResourceGroupName $rgNameGateway
Write-Host ($writeEmptyLine + "# Current sessions variable created" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Disconnect all current sessions
Foreach ($currentSession in $currentSessions) {
Disconnect-AzVirtualNetworkGatewayVpnConnection -VirtualNetworkGatewayName $gatewayName -ResourceGroupName $rgNameGateway `
-VpnConnectionId $currentSession.VpnConnectionId | Out-Null
Write-Host ($writeEmptyLine + "# Session with VpnConnectionID $($currentSession.VpnConnectionId) disconnected" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine
}
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Write script completed
Write-Host ($writeEmptyLine + "# Script completed. Wait at least 5 minutes to validate that all sessions are disconnected" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------