1

I have a PowerShell script

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

It calls WinSCP command file

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

I want to convert "J:\Work\SAS Data\Roman\Lists" to a parameter in the PowerShell script where the parameter can be passed in the txt file. Same goes for the file target.csv. Any help appreciated.

1

1 Answer 1

4

The easiest way (and one that doesn't involve writing a temporary script file) would be to switch to /command for WinSCP:

# 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'

Now you have a much easier time incorporating script parameters:

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'

However, you can of course, still write a command file and pass that:

$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
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for the swift method. However, I get an error as follows for the second script. Error changing directory to $Path
$Path shouldn't even appear in the commands WinSCP gets. Did you use single quotes instead of double quotes by any chance?
Yup that's it. Thanks @Joey !
Hey @Joey, it prompts an error 'too many parameters for command lcd'. Do you know any solution around this?
@KaranPappala: Are you sure your original code worked? Since you already were using lcd with a path that contains spaces, I was assuming things to work. It appears that you have to quote such names, then. I'll edit, accordingly.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.