如何在WinSCP命令中执行的PowerShell脚本中传递参数?

时间:2016-11-16 09:27:51

标签: powershell winscp

我有一个PowerShell脚本

cd "C:\Program Files (x86)\WinSCP"
. .\WinSCP.exe /console /script="L:\Work\SAS Data\FTP\local2remote.txt"  /log=log.txt

它调用WinSCP命令文件

option batch on
open ftp://user:pass@host -passive=on
lcd "J:\Work\SAS Data\Roman\Lists"
put target.csv
exit

我想转换" J:\ Work \ SAS Data \ Roman \ Lists"到PowerShell脚本中可以在txt文件中传递参数的参数。文件target.csv也是如此。任何帮助赞赏。

1 个答案:

答案 0 :(得分:4)

最简单的方法(以及不涉及编写临时脚本文件的方法)将切换到WinSCP的/command

# You don't need the . operator to run external programs
.\WinSCP.exe /console /log=log.txt /command `
  'open ftp://user:pass@host -passive=on' `
  'lcd "J:\Work\SAS Data\Roman\Lists"' `
  'put target.csv' `
  'exit'

现在您可以更轻松地合并脚本参数:

param([string] $Path, [string] $FileName)

& 'C:\Program Files (x86)\WinSCP\WinSCP.exe' /console /log=log.txt /command `
  'open ftp://user:pass@host -passive=on' `
  "lcd `"$Path`"" `
  "put `"$FileName`"" `
  'exit'

但是,当然可以,仍然可以编写命令文件并传递:

$script = Join-Path $Env:TEMP winscp-commands.txt

"open ftp://user:pass@host -passive=on
lcd ""$Path""
put ""$FileName""
exit" | Out-File -Encoding Default $script

& 'C:\Program Files (x86)\WinSCP\WinSCP.exe' /console /log=log.txt /script=$script
Remove-Item $script