Calling multiple commands from powershell, for example psftp

I am trying an SFTP file from powershell using psftp.exe (putty). I can run one command, such as open, but I need to change the default directory and then put the file. The following code leads me to psftp, but ignores the lines of cd .. bye. I think I can run a batch file with sftp commands, but if possible I want to use powershell.

$file = "C:\Source\asdf.csv" $user = "user" $pass = "pass" $hst = "host" $path="C:\ProgramFiles\psftp.exe" $cmd = @" -pw $pass $user@ $hst cd .. cd upload put $file bye "@ invoke-expression "$path $cmd" 
+6
source share
1 answer

Try:

 $file = "C:\Source\asdf.csv" $user = "user" $pass = "pass" $hst = "host" $path="C:\ProgramFiles\psftp.exe" $cmd = @( "cd ..", "cd upload", "put $file", "bye" ) $cmd | & $path -pw $pass " $user@ $hst" 

In response to questions in the comment:

The first part, "$ cmd |" prints the contents of $ cmd to the following command. Since this is an external program (unlike a cmdlet or function), it will send the contents of $ cmd to the stdin of the external program.

The & $ path part says that it treats the contents of $ path as the name of a command or program and executes it.

The rest of the line is passed to the external program as command line arguments.

+6
source

Source: https://habr.com/ru/post/904049/


All Articles