# encoding: ascii
# api: powershell
# title: Putty Port Forward Helpe
# description: Two simple functions to get and set the SSH tunnels that putty will establish
# version: 0.1
# type: function
# author: CrazyDave
# license: CC0
# x-poshcode-id: 6545
# x-derived-from-id: 6546
# x-archived: 2017-03-18T01:01:43
# x-published: 2017-10-04T18:39:00
#
#
 
function AddPortForward {
    param(
        [String] $Connection,
        [UInt16] $SrcPort,
        [String] $DstHost,
        [UInt16] $DstPort
    )
    $baseSessionFolder = 'Software\SimonTatham\PuTTY\Sessions'
    $key = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($baseSessionFolder + "\" + $Connection, $true)
    $currentValue = $key.GetValue("PortForwardings")
    $newValue = $currentValue + "L" + $SrcPort + "=" + $DstHost + ":" + $DstPort + ","
    $key.SetValue("PortForwardings", $newValue, [Microsoft.Win32.RegistryValueKind]::String)
 }

function GetPortForwards {
    $baseSessionFolder = 'HKCU:\Software\SimonTatham\PuTTY\Sessions'
    gci $baseSessionFolder | % {
    $key = $_
    $key.GetValue("PortForwardings").Split(',', [System.StringSplitOptions]::RemoveEmptyEntries) | % {
        $parts = $_.Split('=')
        New-Object PSObject -Property @{
            Connection = $key.PSChildName
            SRC = $parts[0]
            DST = $parts[1]
        }
    }
 }
}
