Recently, I had to download several different items from the same sftp server and after a while I felt like automating it since it wouldn’t be the last time I needed to do that. WinSCP has a cool feature that generates a script for several tasks you’re doing. You can check the function out here.
Anyway here’s my script. Make sure to customize the green parts first. Generate those parts yourself in WinSCP with the generate code function linked above. Also, of course you need to have WinSCP installed on your system.
# Variables
$MyLog = "C:\Temp\Logfile.log"
# Cleaning up previous log files
if (Test-Path "$MyLog"){
Remove-Item "$MyLog"
}
# This function shows a basic menu
function Show-Menu
{
param (
[string]$Title = 'My Download Menu'
)
Clear-Host
Write-Host "================ $Title ================"
Write-Host "1: Press '1' for downloading A."
Write-Host "2: Press '2' for downloading B."
Write-Host "3: Press '3' for downloading C."
Write-Host "Q: Press 'Q' to quit."
}
# This do-loop loops through the menu until 'Q' is pressed.
do {
Show-Menu –Title "$Title"
# Start the script with chosen number
$MySelection = Read-Host "Please make a selection"
switch ($MySelection)
{
'1' {
'You chose option #1 - Downloading A'
& "C:\Program Files (x86)\WinSCP\WinSCP.com" `
/log=$MyLog /ini=nul `
/command `
"open sftp://USERNAME:PASSWORD@SERVERADDRESS/"
"cd FOLDERPATH" `
"lcd %userprofile%\Downloads" `
"get *" `
"exit"
} '2' {
'You chose option #2 - Downloading B'
& "C:\Program Files (x86)\WinSCP\WinSCP.com" `
/log="$MyLog" /ini=nul `
/command `
"open sftp://USERNAME:PASSWORD@SERVERADDRESS/"
"cd FOLDERPATH" `
"lcd %userprofile%\Downloads" `
"get *" `
"exit"
} '3' {
'You chose option #3 - Downloading C'
& "C:\Program Files (x86)\WinSCP\WinSCP.com" `
/log="$MyLog" /ini=nul `
/command `
"open sftp://USERNAME:PASSWORD@SERVERADDRESS/"
"cd FOLDERPATH" `
"lcd %userprofile%\Downloads" `
"get *" `
"exit"
} 'Q' {
return
}
}
}
until ($MySelection -eq 'Q')