From 256f106afae18cdd541b763e0b2035d54c5c60f2 Mon Sep 17 00:00:00 2001 From: Gildas Cherruel Date: Mon, 1 Jan 2024 23:43:44 +0900 Subject: [PATCH 1/4] cosmetics --- Connect-VPN.ps1 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Connect-VPN.ps1 b/Connect-VPN.ps1 index 2369293..d7c0348 100644 --- a/Connect-VPN.ps1 +++ b/Connect-VPN.ps1 @@ -42,7 +42,7 @@ function Connect-AnyConnect() # {{{ # First Stop any VPN cli and ui # There must be only one "client" running when connecting - Get-Process | Where ProcessName -match 'vpn(ui|cli)' | ForEach { + Get-Process | Where-Object ProcessName -match 'vpn(ui|cli)' | ForEach-Object { if (! $_.HasExited) { Write-Verbose "Stopping process $($_.Name) (pid: $($_.Id))" @@ -89,8 +89,7 @@ function Connect-AnyConnect() # {{{ } Write-Verbose "Reading its output stream" - $found = $false - for ($output = $vpncli.StandardOutput.ReadLine(); $output -ne $null; $output = $vpncli.StandardOutput.ReadLine()) + for ($output = $vpncli.StandardOutput.ReadLine(); $null -ne $output; $output = $vpncli.StandardOutput.ReadLine()) { Write-Debug $output if ($output -eq ' >> Login failed.') From 9ab55f8a8d4118d546220e26f40050770e7addcb Mon Sep 17 00:00:00 2001 From: Gildas Cherruel Date: Tue, 2 Jan 2024 21:07:47 +0900 Subject: [PATCH 2/4] Issue #5: Added a timeout for connecting --- Connect-VPN.ps1 | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Connect-VPN.ps1 b/Connect-VPN.ps1 index d7c0348..622536e 100644 --- a/Connect-VPN.ps1 +++ b/Connect-VPN.ps1 @@ -14,7 +14,9 @@ function Connect-AnyConnect() # {{{ [Parameter(Position=3, ParameterSetName='Plain', Mandatory=$true)] [string] $Password, [Parameter(Position=4, Mandatory=$false)] - [switch] $AcceptNotice + [switch] $AcceptNotice, + [Parameter(Position=5, Mandatory=$false)] + [int] $Timeout = 60 ) if ($PSCmdlet.ParameterSetName -eq 'Credential') { @@ -88,8 +90,9 @@ function Connect-AnyConnect() # {{{ $vpncli.StandardInput.WriteLine("y") } + $timer = [Diagnostics.Stopwatch]::StartNew() Write-Verbose "Reading its output stream" - for ($output = $vpncli.StandardOutput.ReadLine(); $null -ne $output; $output = $vpncli.StandardOutput.ReadLine()) + for ($output = $vpncli.StandardOutput.ReadLine(); $null -ne $output -and $timer.Elapsed.TotalSeconds -lt $Timeout; $output = $vpncli.StandardOutput.ReadLine()) { Write-Debug $output if ($output -eq ' >> Login failed.') @@ -110,6 +113,11 @@ function Connect-AnyConnect() # {{{ } } } + $timer.Stop() + if ($timer.Elapsed.TotalSeconds -ge $Timeout) + { + Throw [System.TimeoutException] "Timeout ($Timeout seconds) while connecting to $ComputerName" + } Start-Process -FilePath (Get-Anyconnect -gui) return [PSCustomObject] @{ @@ -149,6 +157,10 @@ function Connect-AnyConnect() # {{{ .PARAMETER AcceptNotice Accept notice from the server, like a banner message. +.PARAMETER Timeout + Maximum time in seconds to wait for the connection to be established. + Default is 60 seconds. + .INPUTS The ComputerName can be piped in. @@ -192,7 +204,9 @@ function Connect-VPN # {{{ [Parameter(Position=4, ParameterSetName='Plain', Mandatory=$true)] [string] $Password, [Parameter(Position=5, Mandatory=$false)] - [switch] $AcceptNotice + [switch] $AcceptNotice, + [Parameter(Position=6, Mandatory=$false)] + [int] $Timeout = 60 ) DynamicParam { From bd0f7c3cf23a6f53bf1ab0c9b79684157580ed1a Mon Sep 17 00:00:00 2001 From: Gildas Cherruel Date: Sat, 27 Jan 2024 17:07:41 +0900 Subject: [PATCH 3/4] Issue #5: Write a nicer error on timeouts --- Connect-VPN.ps1 | 3 ++- README.md | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Connect-VPN.ps1 b/Connect-VPN.ps1 index 622536e..ed1afd5 100644 --- a/Connect-VPN.ps1 +++ b/Connect-VPN.ps1 @@ -116,7 +116,8 @@ function Connect-AnyConnect() # {{{ $timer.Stop() if ($timer.Elapsed.TotalSeconds -ge $Timeout) { - Throw [System.TimeoutException] "Timeout ($Timeout seconds) while connecting to $ComputerName" + Write-Error "Timeout ($Timeout seconds) while connecting to $ComputerName" + return [PSCustomObject] @{} } Start-Process -FilePath (Get-Anyconnect -gui) diff --git a/README.md b/README.md index 4786125..8c19089 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,11 @@ PS> $creds = Get-Credential ACME\gildas PS> $vpn = Connect-VPN -Provider AnyConnect -ComputerName vpn.acme.com -Credential $creds ``` +You can also specify a custom timeout. Upon reaching the timeout the `Connect-VPN` Cmdlet will display an error: +```posh +PS> $vpn = Connect-VPN -Provider AnyConnect -ComputerName vpn.acme.com -Credential $creds -Timeout 60 +``` + In both cases, you can use the TAB completion for the ComputerName. The values come from the available profiles/servers/connections for the given provider. To get the status of a VPN session: From 5e6b3d000ff1bdec356e95628d429b320c2a4b0e Mon Sep 17 00:00:00 2001 From: Gildas Cherruel Date: Sat, 27 Jan 2024 17:26:44 +0900 Subject: [PATCH 4/4] Bumped version to 0.1.6 --- Install.ps1 | 2 +- Posh-VPN.psd1 | 2 +- README.md | 2 +- VERSION | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Install.ps1 b/Install.ps1 index aba42de..a794430 100644 --- a/Install.ps1 +++ b/Install.ps1 @@ -5,7 +5,7 @@ Param( ) $ModuleName = 'Posh-VPN' -$ModuleVersion = '0.1.5' +$ModuleVersion = '0.1.6' $GithubRoot = "https://raw.githubusercontent.com/gildas/posh-vpn/$ModuleVersion" if ([string]::IsNullOrEmpty($Path)) diff --git a/Posh-VPN.psd1 b/Posh-VPN.psd1 index da4f163..93437c5 100644 --- a/Posh-VPN.psd1 +++ b/Posh-VPN.psd1 @@ -12,7 +12,7 @@ RootModule = 'Posh-VPN.psm1' # Version number of this module. -ModuleVersion = '0.1.5' +ModuleVersion = '0.1.6' # ID used to uniquely identify this module GUID = 'c762b59e-61f1-4411-9c5b-3709a00199c1' diff --git a/README.md b/README.md index 8c19089..5fb5fac 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Install-Module Posh-VPN Copy the following line and paste it in a Powershell: ```posh -Start-BitsTransfer http://tinyurl.com/posh-vpn-0-1-5 $env:TEMP ; & $env:TEMP\Install.ps1 +Start-BitsTransfer https://raw.githubusercontent.com/gildas/posh-vpn/0.1.6/Install.ps1 $env:TEMP ; & $env:TEMP\Install.ps1 ``` To install the latest development version, use one of the followings: diff --git a/VERSION b/VERSION index 9faa1b7..c946ee6 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.5 +0.1.6