从Powershell调用多个命令,例如psftp [英] Call multiple commands from powershell e.g psftp

查看:96
本文介绍了从Powershell调用多个命令,例如psftp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用psftp.exe(putty)从Powershell SFTP发送文件。我可以运行诸如open之类的单个命令,但是我需要更改默认目录,然后放入文件。以下代码将我带到psftp,但忽略了从cd ..到再见的行。我想我可以运行一个包含sftp命令的批处理文件,但是如果可能的话,我想使用powershell完成。

I am trying to SFTP a file from powershell using psftp.exe (putty). I can run single command such as open but I need to change the default directory and then put the file. The following code takes me to psftp but ignores lines from cd .. to bye. I guess I can run a batch file which has the sftp commands but if possible I want to accomplish using 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"


推荐答案

尝试一下:

$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"

回答评论中的问题:

第一部分 $ cmd |将$ cmd的内容通过管道传递给随后的命令。由于它是一个外部程序(而不是cmdlet或函数),它将把$ cmd的内容发送到外部程序的stdin。

The first part, "$cmd |" pipes the contents of $cmd to the command that follows. Since it is an external program (as opposed to a cmdlet or function) it will send the contents of $cmd to stdin of the external program.

& $路径部分表示将$ path的内容视为命令或程序名称并执行。

The "& $path" part says to treat the contents of $path as a command or program name and execute it.

该行的其余部分作为命令行传递给外部程序参数。

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

这篇关于从Powershell调用多个命令,例如psftp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆