Search code examples
powershellwinscp

WinSCP: How do you add a PowerShell variable to a WinSCP PowerShell script?


I'm trying to build log path and lcd variables in my WinSCP script and I don't know the proper syntax to make it work.

How can I put the variables into the script correctly? This is an example of what I'm trying to do.

$logPath = "c:\LogPath"
$lcdPath = "c:\lcdPath"

& "C:\Program Files (x86)\WinSCP\WinSCP.com" `  
  /log = $logPath /ini=nul `
  /command `
    "open ftpSite -hostkey=`"`"hostKey`"`" -rawsettings FSProtocol=2" `
    "cd ftpFilePath" `
    $lcdPath `
    "get * -filemask=*>=1D" `
    "exit"

Solution

  • Your syntax for using variables is correct. It's your WinSCP command-line syntax that is wrong.

    1. There should be no spaces around switch values. And the values should better be quoted, in case they contain spaces:

      /log="$logPath"
      
    2. The /log takes path to a file, not path to a directory:

      $logPath = "c:\LogPath\WinSCP.log"
      
    3. You are missing the actual lcd command:

      "lcd $lcdPath" `
      
    4. The only PowerShell issue I see in your code is that the backtick that is meant to escape a line end, has to be the last character on the line. While you have spaces after the backtick in the WinSCP.com line:

      & "C:\Program Files (x86)\WinSCP\WinSCP.com" `#no spaces here