2016-11-16 157 views
0

我有一个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:[email protected] -passive=on 
lcd "J:\Work\SAS Data\Roman\Lists" 
put target.csv 
exit 

我想转换 “j:\工作\ SAS数据\罗马\列表” 的参数在PowerShell脚本该参数可以在txt文件中传递。文件target.csv也一样。任何帮助赞赏。

+0

您最好使用[PowerShell脚本](https://winscp.net/eng/docs/library_powershell)中的[WinSCP .NET程序集](https://winscp.net/eng/docs/library)。 –

回答

3

最简单的方法(和一个不涉及编写一个临时脚本文件)会切换到/command为的WinSCP:

# You don't need the . operator to run external programs 
.\WinSCP.exe /console /log=log.txt /command ` 
    'open ftp://user:[email protected] -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:[email protected] -passive=on' ` 
    "lcd `"$Path`"" ` 
    "put `"$FileName`"" ` 
    'exit' 

但是,你当然可以,还是写命令文件和传递:

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

"open ftp://user:[email protected] -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 
+0

谢谢你的快速方法。但是,第二个脚本出现以下错误。将目录更改为$ Path时出错 –

+0

'$ Path'甚至不应出现在WinSCP获取的命令中。你有没有使用单引号而不是双引号? – Joey

+0

是的,就是这样。谢谢@Joey! –

相关问题