-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet-VpnState.ps1
70 lines (63 loc) · 1.65 KB
/
Set-VpnState.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
# Set-VpnState.ps1
# Purpose: Toggle, start or stop the windows service "PanGPS" (GlobalProtect VPN)
# based on the given input.
# Requires: PowerShell, Administrator privileges
# Usage: Set-VpnState ([state])
# Set-VpnState (restart|off|stop|on|start)
# Examples:
# Set-VpnState
# Set-VpnState on
# Set-VpnState off
# Set-VpnState restart
param(
[string] $State = $null
, [switch] $NoDelay
)
function Get-ServiceScript {
param( [string] $ServiceName )
Get-Service $ServiceName -ErrorAction SilentlyContinue
}
$serviceName = "PanGPS"
$service = Get-ServiceScript $serviceName
$secondsToWait = 5
if ($service) {
Write-Output "$serviceName status was '$($service.Status)'"
if ($State) {
Write-Output "Desired state: '$State'"
if ($State -like "off" -or $State -like "stop") {
Write-Output "Stopping now...."
Stop-Service $serviceName #-Force
}
elseif ($State -like "on" -or $State -like "start") {
Write-Output "Starting now...."
Start-Service $serviceName #-Force
}
elseif ($State -like "restart") {
Write-Output "Restarting now...."
Restart-Service $serviceName #-Force
}
else {
Write-Output "Invalid state: '$State'"
}
}
else {
if ($service.Status -like "Running") {
Write-Output "Stopping now...."
Stop-Service $serviceName #-Force
}
elseif ($service.Status -notlike "Running") {
Write-Output "Starting now...."
Start-Service $serviceName #-Force
}
}
}
else {
Write-Output "Service not found: '$serviceName'"
}
if (!$NoDelay) {
Write-Output ""
Write-Output "Done"
Write-Output ""
Write-Output "Exiting in $secondsToWait seconds...."
Start-Sleep -Seconds $secondsToWait
}