Functions/Public/Merge-PSPuTTYTheme.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
using namespace System.Web

function Merge-PSPuTTYTheme {
    <#
    .Synopsis
    Configures a PuTTY saved session with the specified theme.

    .Parameter SessionName
    The name of the PuTTY saved session that will have its theme updated.
    
    NOTE: The session name will be URL-encoded, to conform to the stored value in the registry. Make sure that you do not use the URL-encoded name of the session.
          Rather, use the "friendly name" of the session, that appears in the PuTTY Saved Session list.

    .Parameter ThemeName
    The name of the PuTTY theme that will be configured on the saved session.

    .Parameter Session
    The PSPuTTYSession object that will be updated with a new theme.
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ParameterSetName = 'SessionNameThemeName')]
        [Parameter(Mandatory = $true, ParameterSetName = 'SessionNameThemeObject')]
        [string] $SessionName
      , [Parameter(Mandatory = $true, ParameterSetName = 'SessionObjectThemeName')]
        [Parameter(Mandatory = $true, ParameterSetName = 'SessionObjectThemeObject')]
        [PSPuTTYSession] $Session
      , [Parameter(Mandatory = $true, ParameterSetName = 'SessionObjectThemeName')]
        [Parameter(Mandatory = $true, ParameterSetName = 'SessionNameThemeName')]
        [string] $ThemeName
      , [Parameter(Mandatory = $true, ParameterSetName = 'SessionNameThemeObject')]
        [Parameter(Mandatory = $true, ParameterSetName = 'SessionObjectThemeObject')]
        [PSPuTTYTheme] $Theme
    )

    Write-Verbose -Message ('Using parameter set: {0}' -f $PSCmdlet.ParameterSetName)

    ### If the user specifies a session name, then we need to URL encode it
    if ($PSCmdlet.ParameterSetName -match 'SessionName') {
      Write-Verbose -Message 'Caller specified session name. Obtaining session object.'
      $Session = Get-PSPuTTYSession -Name $SessionName
    }

    if ($PSCmdlet.ParameterSetName -match 'ThemeName') {
      Write-Verbose -Message 'Caller specified theme name. Obtaining theme object.'
      $Theme = Get-PSPuTTYTheme -Name $ThemeName
    }

    Write-Verbose -Message ($Session)
    Write-Verbose -Message ('Opening PuTTY session subkey: {0}' -f $Session.Name)
    $PuTTYSessionReg = (Get-PSPuTTYRegistrySessions).OpenSubkey($Session.Name, $true)

    $SessionKey = $PuTTYSessionReg.OpenSubkey($Session.Name)
    foreach ($Number in 0..21) {
      Write-Verbose -Message ('Setting Colour{0} to {1}' -f $Number, $Theme."Colour$Number")
      $PuTTYSessionReg.SetValue("Colour$Number", $Theme."Colour$Number", [Microsoft.Win32.RegistryValueKind]::String)
    }

    $PuTTYSessionReg.SetValue('TerminalType', 'putty-256color')
}