2016-11-16 47 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! –

相關問題