-
Notifications
You must be signed in to change notification settings - Fork 5
/
Disconnect-VPN.ps1
104 lines (92 loc) · 2.75 KB
/
Disconnect-VPN.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
function Disconnect-AnyConnect() # {{{
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[PSCustomObject] $VPNSession
)
Write-Verbose "Starting the VPN cli"
$vpncli = New-Object System.Diagnostics.Process
$vpncli.StartInfo = New-Object System.Diagnostics.ProcessStartInfo(Get-AnyConnect)
$vpncli.StartInfo.Arguments = "disconnect"
$vpncli.StartInfo.CreateNoWindow = $false
$vpncli.StartInfo.UseShellExecute = $false
$vpncli.StartInfo.RedirectStandardOutput = $true
$vpncli.StartInfo.RedirectStandardError = $true
$vpncli.Start() | Out-Null
Write-Verbose "Reading its output"
for ($output = $vpncli.StandardOutput.ReadLine(); $output -ne $null; $output = $vpncli.StandardOutput.ReadLine())
{
Write-Debug $output
if ($output -match ' >> note: (.*)')
{
Write-Warning $matches[1]
}
elseif ($output -match ' >> state: (.*)')
{
Write-Verbose $matches[1]
}
}
for ($output = $vpncli.StandardError.ReadLine(); $output -ne $null; $output = $vpncli.StandardError.ReadLine())
{
Write-Warning $output
}
} #}}}
<#
.SYNOPSIS
Disconnect from a VPN Session or Provider.
.DESCRIPTION
Disconnect this computer from a given VPN Session or Provider.
.NOTES
Only Cisco AnyConnect VPNs are supported as of now.
.PARAMETER Provider
The VPN Provider to use.
One of: AnyConnect
.PARAMETER VPNSession
The VPN session object returned by Connect-VPN.
.LINK
https://github.com/gildas/posh-vpn
.EXAMPLE
$session = Connect-VPN -Provider AnyConnect -ComputerName vpn.acme.com -Credentials (Get-Credential ACME\gildas)
Disconnect-VPN $session
Description
-----------
Disconnects from a Cisco AnyConnect VPN session
.EXAMPLE
Disconnect-VPN -Provider AnyConnect
Description
-----------
Disconnects from any Cisco AnyConnect VPN
#>
function Disconnect-VPN() # {{{
{
[CmdletBinding(DefaultParameterSetName='Session')]
Param(
[Parameter(Position=1, ParameterSetName='Session', Mandatory=$true)]
[PSCustomObject] $VPNSession,
[Parameter(Position=1, ParameterSetName='Provider', Mandatory=$true)]
[ValidateSet('AnyConnect')]
[string] $Provider
)
switch($PSCmdlet.ParameterSetName)
{
'Session'
{
switch($VPNSession.Provider)
{
'AnyConnect' { Disconnect-AnyConnect @PSBoundParameters }
$null { Throw [System.ArgumentException] "VPNSession misses a Provider"; }
default { Throw "Unsupported VPN Provider: $VPNSession.Provider" }
}
}
'Provider'
{
$PSBoundParameters.Remove('Provider') | Out-Null
switch($Provider)
{
'AnyConnect' { Disconnect-AnyConnect @PSBoundParameters }
default { Throw "Unsupported VPN Provider: $VPNSession.Provider" }
}
}
}
} # }}}