Differences

This shows you the differences between the selected revisions of the page.

scripting 2019-06-19 scripting 2024-10-03 (current)
Line 31: Line 31:
To further analyze results of scripted operations, you will find [[logging_xml|XML logging]] useful. To further analyze results of scripted operations, you will find [[logging_xml|XML logging]] useful.
-//For more details, refer to [[faq_script_result|How do I know that script completed successfully?]]//+//For more details, refer to [[faq_script_result|*]]//
Line 68: Line 68:
==== [[timestamp]] Timestamp ==== ==== [[timestamp]] Timestamp ====
-WinSCP automatically resolves ''%TIMESTAMP[rel]#format%'' to a real time (optionally to a past or future time) with the given format. The ''format'' may include ''yyyy'' for year, ''mm'' for month, ''dd'' for day, ''hh'' for hour, ''nn'' for minute and ''ss'' for second. For example, the ''%TIMESTAMP#yyyy-mm-dd%'' resolves to ''2016-06-22'' on 22 June 2016. See [[http://docwiki.embarcadero.com/Libraries/en/System.SysUtils.FormatDateTime|other formats you can use]].+WinSCP automatically resolves ''%TIMESTAMP[rel]#format%'' to a real time (optionally to a past or future time) with the given format. The ''format'' may include ''yyyy'' for year, ''mm'' for month, ''dd'' for day, ''hh'' for hour, ''nn'' for minute and ''ss'' for second. For example, the ''%TIMESTAMP#yyyy-mm-dd%'' resolves to ''2016-06-22'' on 22 June 2016. See [[https://docwiki.embarcadero.com/Libraries/en/System.SysUtils.FormatDateTime|other formats you can use]].
The optional ''rel'' part, with syntax ''[-+]time[YDHNS]'', produces past (''-'') or future (''+'') timestamps. One of the following units must be used: ''Y'' (years), ''D'' (days), ''H'' (hours), ''N'' (minutes) or ''S'' (seconds). For example, the ''%TIMESTAMP-1D#yyyy-mm-dd%'' (the ''-1D'' meaning one day in the past) resolves to ''2016-06-21'' on 22 June 2016. The optional ''rel'' part, with syntax ''[-+]time[YDHNS]'', produces past (''-'') or future (''+'') timestamps. One of the following units must be used: ''Y'' (years), ''D'' (days), ''H'' (hours), ''N'' (minutes) or ''S'' (seconds). For example, the ''%TIMESTAMP-1D#yyyy-mm-dd%'' (the ''-1D'' meaning one day in the past) resolves to ''2016-06-21'' on 22 June 2016.
Line 85: Line 85:
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 properly.((This is important particularly for FTP sessions.)) 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 properly.((This is important particularly for FTP sessions.))
-==== Comments ====+==== [[comments]] Comments ====
To insert comments into the script file, start the line with ''#'' (hash): To insert comments into the script file, start the line with ''#'' (hash):
Line 130: Line 130:
Learn about ''[[executables|winscp.com]]'', the console interface tool. Learn about ''[[executables|winscp.com]]'', the console interface tool.
-# Connect +===== [[hostkey]] Verifying the Host Key or Certificate in Script ===== 
-open sftp://sapefi:Spe$f694@172.17.1.36/ -hostkey="ssh-rsa 2048 xxxxxxxxxxx...=" +The first connection to an SSH server requires [[ssh_verifying_the_host_key|verification of the host key]]. To automate the verification in script, use ''[[scriptcommand_open#hostkey|-hostkey]]'' switch of ''[[scriptcommand_open|open]]'' command to accept the expected host key automatically. 
-# Change remote directory + 
-cd /home/user +You can find the key fingerprint on [[ui_fsinfo|Server and Protocol Information Dialog]]. You can also copy the key fingerprint to clipboard from the [[ssh_verifying_the_host_key|confirmation prompt]] on the first (interactive) connection using //Copy key fingerprints to clipboard// command (in the script, use SHA-256 fingerprint of the host key only). //Learn more about [[faq_hostkey|obtaining host key fingerprint]]//. 
-# Download file to the local directory d:\ + 
-get examplefile.txt d:\ +FTPS/WebDAVS [[tls#certificate|TLS/SSL certificate]] signed by untrusted authority may also need to be verified. To automate the verification in script, use ''[[scriptcommand_open#certificate|-certificate]]'' switch of ''[[scriptcommand_open|open]]'' command to accept the expected certificate automatically.
-# Disconnect +
-close +
-# Connect as a different user +
-open sftp://sapefi:Spe$f694@sftp2.manipalgroup.info/ +
-# Change the remote directory +
-cd /home/user2 +
-# Upload the file to current working directory +
-put d:\examplefile.txt  +
-# Disconnect +
-close +
-# Exit WinSCP +
-exit+
===== [[different_account]] Running a Script under a Different Account (e.g., Using a Scheduler) ===== ===== [[different_account]] 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 [[config|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 [[config|INI file]] instead. +If you are going to run the script under a different account (for example [[guide_schedule|using the Windows Task Scheduler]]), make sure the script does not rely on a configuration settings that might differ on the other account. When using registry as [[config|configuration storage]], the settings are accessible only for your Windows account. Ideally, make sure the script does not rely on any external configuration, to make it completely portable. Note that the configuration also includes [[#hostkey|verified SSH host keys]] and FTPS/WebDAVS [[tls#certificate|TLS/SSL certificates]].
-Note that the configuration also includes [[#hostkey|verified SSH host keys]] and FTPS/WebDAVS [[tls#certificate|TLS/SSL certificates]]. +//For details, see the [[#configuration|next section]] and [[faq_environment|*]]//
===== [[configuration]] Sharing Configuration with Graphical Mode ===== ===== [[configuration]] Sharing Configuration with Graphical Mode =====
Line 163: Line 151:
If you want to protect your script from such inadvertent change or if you want to make the script portable, you should isolate its configuration from graphical mode explicitly. If you want to protect your script from such inadvertent change or if you want to make the script portable, you should isolate its configuration from graphical mode explicitly.
-The best way to do that is to configure all the options you need using script commands only (''[[scriptcommand_option|option]]'' command, switches of other commands, [[session_url|session URL]]), or if no such command is available, using [[rawsettings|raw site settings]] and [[rawconfig|raw configuration]]. Finally force scripting mode to start with the default configuration using ''[[commandline#configuration|/ini=nul]]'' command-line parameter.+The best way to do that is to configure all the options you need using script commands only (''[[scriptcommand_option|option]]'' command, switches of other commands, [[session_url|session URL]]), or if no such command is available, using [[rawsettings|raw site settings]] and [[rawconfig|raw configuration]]. Finally force scripting mode to [[config#no|start with the default configuration]] using ''[[commandline#configuration|/ini=nul]]'' command-line parameter.
Alternatively [[config#export|export your configuration]] to a separate INI file and reference it using ''[[commandline#configuration|/ini=]]'' command-line parameter. Also consider setting the INI file read-only, to prevent WinSCP writing to it, when exiting. Particularly, if you are running multiple scripts in parallel, to prevent different instances of WinSCP trying to write it at the same time. Alternatively [[config#export|export your configuration]] to a separate INI file and reference it using ''[[commandline#configuration|/ini=]]'' command-line parameter. Also consider setting the INI file read-only, to prevent WinSCP writing to it, when exiting. Particularly, if you are running multiple scripts in parallel, to prevent different instances of WinSCP trying to write it at the same time.
Line 175: Line 163:
<code winscp> <code winscp>
# Connect # Connect
-open sftp://user:password@example.com/ -hostkey="ssh-rsa 2048 xxxxxxxxxxx...="+open sftp://user:password@example.com/ -hostkey="ssh-rsa 2048 xxxxxxxxxxx..."
# Change remote directory # Change remote directory
cd /home/user cd /home/user
Line 200: Line 188:
For simple scripts you can specify all the commands on [[commandline|command-line]] using ''/command'' switch: For simple scripts you can specify all the commands on [[commandline|command-line]] using ''/command'' switch:
<code batch> <code batch>
-winscp.com /ini=nul /command "open sftp://user:password@example.com/ -hostkey=""ssh-rsa 2048 xxxxxxxxxxx...=""" "get examplefile.txt d:\" "exit"+winscp.com /ini=nul /command "open sftp://user:password@example.com/ -hostkey=""ssh-rsa 2048 xxxxxxxxxxx...""" "get examplefile.txt d:\" "exit"
</code> </code>
Line 207: Line 195:
<code batch> <code batch>
winscp.com /ini=nul /command ^ winscp.com /ini=nul /command ^
-    "open sftp://user:password@example.com/ -hostkey=""ssh-rsa 2048 xxxxxxxxxxx...=""" ^+    "open sftp://user:password@example.com/ -hostkey=""ssh-rsa 2048 xxxxxxxxxxx...""" ^
    "get examplefile.txt d:\" ^     "get examplefile.txt d:\" ^
    "exit"     "exit"

Last modified: by 125.22.91.98