Thursday, November 17, 2016

How to export PuTTY Sessions list and create executable PuTTY Batch file/PowerShell Script.

First start by exporting a currently configured Session from the registry to a REG file.




1. regedit /e "%userprofile%\Documents\ESX-1.reg" HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions


1.----------------------BATCH FILE TO CALL SINGLE HOST-----------------------------
@echo off
import %USERPROFILE%\Documents\ESX-1.reg
cd "C:\Program Files (x86)\PuTTY\"
start putty.exe -load ESX-1
timeout /t 2
reg delete HKCU\SOFTWARE\SimonTatham\PuTTY\Sessions\ESX-1 /f
-----------------------------------------------------------------------------------------------------




2. regedit /e "%userprofile%\Documents\Sessions.reg" HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions


2.---------------------BATCH FILE TO ADD MULTIPLE HOSTS IN PUTTY-----------
@echo off
import %USERPROFILE%\Documents\SESSIONS.reg
cd "C:\Program Files (x86)\PuTTY\"
start putty.exe -load SESSIONS
------------------------------------------------------------------------------------------------------




-----------------------------PowerShell Script to EXPORT all Sessions--------------------------
# All settings


$registry_path = "HKCU:\Software\SimonTatham"


# Only sessions


#$registry_path = "HKCU:\Software\SimonTatham\PuTTY\Sessions"
$output_file = "Sessions.reg"


$registry = ls "$registry_path" -Recurse


"Windows Registry Editor Version 5.00" | Out-File Sessions.reg


"" | Out-File Sessions.reg -Append


foreach ($reg in $registry) {

"[$reg]" | Out-File Sessions.reg -Append


foreach ($prop in $reg.property) {

$propval = $reg.GetValue($prop)

if ("".GetType().Equals($propval.GetType())) {

'"' + "$prop" + '"' + "=" + '"' + "$propval" + '"' | Out-File Sessions.reg -Append


} elseif ($propval -is [int]) {

$hex = "{0:x8}" -f $propval


'"' + "$prop" + '"' + "=dword:" + $hex | Out-File Sessions.reg -Append



}


}
"" | Out-File Sessions.reg -Append

}
---------------------------------------------------------------------------------------------


-----------------------PowerSHell Script to IMPORT Sessions list-----------------------------
$input_file = "Sessions.reg"


$content = Get-Content "$input_file"



"Push-Location"


"cd HKCU:\"
foreach ($line in $content) {

If ($line.StartsWith("Windows Registry Editor")) {

# Ignore the header


} ElseIf ($line.startswith("[")) {

$section = $line.Trim().Trim('[', ']')

'New-Item -Path "' + $section + '" -Force' | %{ $_ -replace 'HKEY_CURRENT_USER\\', '' }

} ElseIf ($line.startswith('"')) {

$linesplit = $line.split('=', 2)

$key = $linesplit[0].Trim('"')

if ($linesplit[1].StartsWith('"')) {

$value = $linesplit[1].Trim().Trim('"')

} ElseIf ($linesplit[1].StartsWith('dword:')) {

$value = [Int32]('0x' + $linesplit[1].Trim().Split(':', 2)[1])

'New-ItemProperty "' + $section + '" "' + $key + '" -PropertyType dword -Force' | %{ $_ -replace 'HKEY_CURRENT_USER\\', '' }

} Else {

Write-Host "Error: unknown property type: $linesplit[1]"


exit

}
'Set-ItemProperty -Path "' + $section + '" -Name "' + $key + '" -Value "' + $value + '"' | %{ $_ -replace 'HKEY_CURRENT_USER\\', '' }




}


}
"Pop-Location"
--------------------------------------------------------------------------------------------------