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.
Your are combining
psftpcommands and PowerShell commands (in your case just thepsftpinvocation) into one file. That cannot work. PowerShell stops on the call to thepsftpand waits for it to finish.psftpon the contrary does not know that the PowerShell script even exist, so it cannot read its commands from there.Put your
psftpcommands (linesopenthoughquit) into a separate file (download.txt) and execute it with the following command:Alternatively, you can feed the commands to
psftpstandard input.A related question:
Batch file for PuTTY/PSFTP file transfer automation