Topic "Using saved sessions in Powershell"

Author Message
philcart
[View user's profile]

Joined: 2014-07-30
Posts: 4
Hi

Currently using WinSCP 5.5.4 and I'm automating our SFTP transfer processes using Powershell. So far, everything is working well so far, but I do have one question.

In the WinSCP GUI we have over a dozen saved sites that are used for a variety of transfer operations. From a command-line, we can use these saved sites with this syntax
Code:
winscp.exe [mysession] /synchronize [local_directory] [remote_directory] [/defaults]


In all the Powershell examples I've seen, the HostName, UserName Password and Host Fingerprint are hardcoded directly into the script. Is there anyway for me to refer to our currently saved sites from Powershell?


Cheers
Phil
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25015
Location: Prague, Czechia
Quote from http://stackoverflow.com/questions/20353558/winscp-net-assembly-open-stored-session:

Quote:
First, you should not do this. Coupling the assembly with WinSCP application configuration can cause you lots of troubles. The assembly is deliberately isolated from WinSCP application by default.

There's nothing that you cannot configure using SessionOptions class. Either using its properties, for basic options; or using SessionOptions.AddRawSettings method, for advanced settings.

Anyway, to answer your question:

- Set Session.DefaultConfiguration to false (to couple the assembly with WinSCP configuration);
- Make sure the assembly shares the configuration with WinSCP. Particularly note that WinSCP uses registry to store configuration by default. So make sure the assembly runs under the same account that you have your stored session/site configured with. Or use INI file instead (use Session.IniFilePath);
- Set SessionOptions.HostName to the name of your stored session/site (this is undocumented and unsupported). Note that this works only, if the name does not contain any characters with special meaning in URLs.

_________________
Martin Prikryl
philcart
[View user's profile]

Joined: 2014-07-30
Posts: 4
Thanks Martin.

Would much prefer a supported solution.

So is anything you could suggest I look into to avoid hard coding hostname, username, password and server fingerprint values in the script?


Cheers
Phil
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25015
Location: Prague, Czechia
Load them from a configuration file in your PowerShell code.
See https://winscp.net/eng/docs/guide_protecting_credentials_for_automation#powershell
_________________
Martin Prikryl
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License