powershellsftppowershell-5.1psftp

PowerShell scripts just hangs without moving to next statement when trying to run psftp commands


I have a following PowerShell scripts which is to get a file from remote Linux server to local directory, using psftp and get commands. These two commands as standalone, works just fine. However, as a following script copy.ps1:

psftp myusername@linux-host
get /tmp/file1 D:\folder\file1.dat
quit 

I get following output:

C:\Users\myusername\Desktop\copy.ps1
psftp : Using username "myusername".
At C:\Users\myusername\Desktop\copy.ps1:1 char:1
+ psftp myusername@linux-host
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Using username "myusername".:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
Pre-authentication banner message from server:
| Authorized uses only. All activity may be monitored and reported.
End of banner message from server
Remote working directory is /home/myusername

So, there are two issues:

First NativeCommandError and then after the output Remote working directory the shell just stays there without moving ahead with get command. I must do ^C to bring the terminal back.


Solution

  • Your are combining psftp commands and PowerShell commands (in your case just the psftp invocation) into one file. That cannot work. PowerShell stops on the call to the psftp and waits for it to finish. psftp on the contrary does not know that the PowerShell script even exist, so it cannot read its commands from there.

    Put your psftp commands (lines open though quit) into a separate file (download.txt) and execute it with the following command:

    "C:\Program Files\PuTTY\psftp.exe -b c:\path\to\download.txt
    

    Alternatively, you can feed the commands to psftp standard input.

    A related question:
    Batch file for PuTTY/PSFTP file transfer automation