This is an old revision of the document!

Scripting and Task Automation

Advertisement

This article contains detailed description of scripting/automation functionality. You may want to see simplified guide to the functionality instead.

In addition to graphical interface, WinSCP offers scripting/console interface with many commands. The commands can be typed in interactively, or read from script file or another source.

Using Scripting

See command-line parameters to learn how to enter the console/scripting mode.

For automation, commands can be read from a script file specified by /script switch, from standard input or passed from the command-line using the /command switch.

By default an interactive mode is used (the user is prompted in the same way as in GUI mode). To switch to a batch mode (all prompts are automatically answered negatively) use the command option batch abort. For the batch mode it is recommended to turn off confirmations using option confirm off to allow overwrites (otherwise the overwrite confirmation prompt would be answered negatively, making overwrites impossible).

Multiple sessions can be opened simultaneously. Use the session command to switch between them.

Note that the first connection to an SSH server requires verification of the host key. Also the first connection to FTPS host with certificate signed by untrusted authority requires verification of the certificate.

Advertisement

Checking Results

WinSCP returns exit code 1, when any command is interrupted due to an error or any prompt is answered Abort (even automatically in batch mode). Otherwise it returns the exit code 0.1

To further analyze results of scripted operations, you will find XML logging useful.

Commands Syntax

All WinSCP commands have syntax:

command -switch -switch2 parameter1 parameter2 ... parametern

Command parameters that include space(s) have to be enclosed in double-quotes. To use double-quote literally, double it:

put "file with spaces and ""quotes"".html"

You can use environment variables in the commands, with syntax %NAME%2:

put "%FILE_TO_UPLOAD%"

You can reference script arguments (passed on command-line using parameter /parameter) using syntax %N%, where N is ordinal number of argument2:

put "%1%"

Note that WinSCP treats filenames in case sensitive manner. So even if your server treats filenames in case insensitive manner, make sure you specify case properly3.

The Console Interface Tool

Learn about winscp.com, the console interface tool.

Verifying the Host Key or Certificate in Script

The first connection to an SSH server requires verification of the host key. To automate the verification in script, use hostkey switch of open command to accept the expected hostkey automatically.

You can find the key fingerprint on Server and Protocol Information Dialog. You can also copy the key fingerprint to clipboard from the confirmation prompt on the first (interactive) connection using Copy Key button.

FTPS certificate signed by untrusted authority may also need to be verified. To automate the verification in script, use certificate switch of open command to accept the expected certificate automatically.

Running a Script under a Different Account (e.g., Using a Scheduler)

If you are going to run the script under a different account (for example using the Windows scheduler), don’t forget that WinSCP still needs to access its configuration. Note that when using registry as configuration storage, the settings are accessible only for your Windows account, so in such a case you may need to either transfer the configuration from your account registry to the other account’s registry or use the INI file instead.

Advertisement

Note that the configuration also includes verified SSH host keys.

  1. When checking WinSCP exit code from batch file, make sure you are using winscp.com.Back
  2. Generally do enclose reference to double-quotes to cope properly with spaces in its value.Back
  3. This is important particularly for FTP sessions.Back

Last modified: by 203.190.151.114