Functions/Public/Get-PSPuTTYSession.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
using namespace Microsoft.Win32
using namespace System.Web

function Get-PSPuTTYSession {
  <#
  .Synopsis
  Returns a list of configured PuTTY sessions.
  
  .Outputs
  [PSPuTTYSession]
  #>

  [CmdletBinding()]
  param (
    [Parameter(Mandatory = $false)]
    [string] $Name
  )

  $Command = $PSCmdlet.MyInvocation.MyCommand.Name
  $Module = $PSCmdlet.MyInvocation.MyCommand.Module

  ### Get a reference to the PuTTY Registry sessions subkey
  $PuTTYSessions = Get-PSPuTTYRegistrySessions

  ### List out the sessions under the PuTTY sessions subkey
  ### NOTE: Sessions are stored as URL-encoded names, and do not have spaces, even if the session name does have spaces.
  ### The PSPuTTYSession data model has properties to store both the URL encoded name, and the URL decoded (natural) session name
  $KeyList = $PuTTYSessions.GetSubKeyNames()

  ### Empty array to hold a list of sessions
  $SessionList = @()

  foreach ($Key in $KeyList) {
    $KeyName = [System.Web.HttpUtility]::UrlDecode($Key) 
    Write-Verbose -Message ('Found PuTTY saved session: {0}, URL decoded: {1}' -f $Key, $KeyName)
    $SessionList += [PSPuTTYSession]::new($Key, $KeyName)
  }

  if ($PSBoundParameters.ContainsKey('Name')) {
    Write-Output -InputObject $SessionList.Where({ $PSItem.RealName -eq $Name })
  } else {
    Write-Output -InputObject $SessionList
  }
}