Generate session URL from command line?

Advertisement

sacklunch
Guest

Generate session URL from command line?

We have an automated sftp process where the username and password change frequently and contain all sorts of special characters. Because of this, we use the WinSCP UI to generate a session URL with the username and password encoded correctly for a PowerShell script, then we manually copy/paste the URL into our scripts.

We have a lot of scripts like this, so I'd like to automate that process of generating an encoded session URL. Is there a way to do that from the command line, with the .exe or .com or some other process?

Reply with quote

Advertisement

sacklunch
Guest

Re: Generate session URL from command line?

Thanks for the reply, and sorry for the delay in responding! I did get a chance to test out Uri.EscapeDataString, but evidently it uses different encoding mechanisms than the WinSCP UI. It produces a slightly different string, which ultimately gives me username/password errors when trying to connect. For example:

Original string: z*2$*(iF65%@Lu^F4@:Eov5F0f@
Encoded with WinSCP UI: z*2%%24*%%28iF65%%25%%40Lu%%5EF4%%40%%3AEov5F0f%%40
Encoded with Uri.EscapeDataString: z%2A2%24%2A%28iF65%25%40Lu%5EF4%40%3AEov5F0f%40

I'm new to scripting with WinSCP, and still feeling my way around things. I'm unfamiliar with SessionOptions.UserName and SessionOptions.Password. Do you happen to have a simple script that shows how those would be used per your suggestion? Sorry, I'm sure that's a basic question, but I appreciate any help! Thanks again.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

Re: Generate session URL from command line?

So it seems that you run WinSCP scripting from your PowerShell script, not WinSCP .NET assembly, right?

The only two differences between the two encodings seems to be that Uri.EscapeDataString encodes also *, what WinSCP UI does not. That's not an issue.

The problem is that your credentials are not encoded only for URL, but also for a batch file, where % has to be doubled to %%. See:
https://winscp.net/eng/docs/session_url#special

In PowerShell, you can do that simply by:
$url = $url -replace "%", "%%"

But on the other hand, you do not need this % encoding in PowerShell. That only for batch files. So I'm bit confused.

Reply with quote

Advertisement

You can post new topics in this forum