2

I want to use PowerShell to connect to a PuTTY "saved session" and then specify a file that contains some batch commands. Using CMD this would look like

d:\putty\psftp 'Saved Session Name' -b d:\location.txt.

I think that the PS equivalent should look like

Start-Process d:\putty\psftp.exe 'Saved Session Name' 
(and then a call to pass a 'get' script) i.e. cd Outgoing get <date>.txt 

However, I get the following error:

a positional parameter cannot be found that accepts the argument

How can I accomplish this using PowerShell?

2
  • 1
    You don't necessarily need Start-Process. What happens when you try to run d:\putty\psftp.exe 'Saved Session Name' -b d:\location.txt from Powershell? The biggest catch is if you have spaces in path names. You might need to use quotes and the call operator: &"d:\putty\psftp.exe" 'Saved Session Name' -b "d:\location.txt".
    – Bacon Bits
    Commented Jun 29, 2015 at 14:40
  • Updated my comment to an answer and added some details.
    – Bacon Bits
    Commented Jun 29, 2015 at 14:59

4 Answers 4

5

All you need is plink:

plink 'Saved Session Name'
2
  • 1
    I get a rubbish terminal: ←[0m←[27m←[24m←[J←[36mroot - raspberrypi ←[33m ~ ←[36m←[00m←[K←[?1h←=←[?2004h \n Due to lack of memory on the rasp or my host system win 10?
    – Timo
    Commented Nov 8, 2020 at 19:37
  • Amazing. With this, I can use PS to SSH to my Linux server using a nicer terminal than CMD prompt.
    – Frantumn
    Commented Feb 7, 2023 at 17:53
1

You don't necessarily need Start-Process.

What happens when you try to run d:\putty\psftp.exe 'Saved Session Name' -b d:\location.txt from Powershell? The first thing I do is try it exactly like I'd run it from the command line.

The biggest catch is if you have spaces in path names. You might need to use quotes and the call operator (ampersand): &"d:\putty\psftp.exe" 'Saved Session Name' -b "d:\location.txt".

If you do need to use Start-Process, you can do this:

Start-Process -FilePath "d:\putty\psftp.exe" `
    -ArgumentList "'Saved Session Name' -b d:\location.txt" -Wait

Or like this:

Start-Process -FilePath "d:\putty\psftp.exe" `
    -ArgumentList 'Saved Session Name', '-b', "d:\location.txt" -Wait

Note that the argument list in the first is a single string with every argument in it, and in the second it is an array of strings with one string for every argument. Everything needs to be in the same order that they'd be on the command line, and it's not uncommon for it to be a bit flaky. Usually one method or the other works better, but it depends on the application you're calling. Usually with quotes and spaces in path names because you're going through multiple levels of escaping depending on the program you're calling (noticing a theme?).

I added the -Wait parameter to my code above because, by default, Start-Process continues to the next line without waiting since it actually spawns a separate process. -Wait forces Powershell to, well, wait, which is what people usually want in a non-interactive script.

See Get-Help about_Operators or Get-Help "call operator" for more topics for help with the call operator. See Get-Help Start-Process for help with that.

1
  • I receive 'unable to open connection to putty_loc -host does not exist.
    – Timo
    Commented Nov 8, 2020 at 19:41
1

Adding the below Technet Wiki link which contains various ways to run executables in PowerShell.

PowerShell: Running Executables

0

Try this:

$Path = "d:\putty\psftp.exe" 
$Prm1 = 'Saved Session Name'
$Prm2 = "-b"
$Prm3 = "d:\location.txt"

&$Path $Prm1 $Prm2 $Prm3

Not the answer you're looking for? Browse other questions tagged or ask your own question.