Unable to log onto SFTP server when using powershell script

Advertisement

Eisman333
Joined:
Posts:
4

Unable to log onto SFTP server when using powershell script

Hey everybody,

I know this topic or similar ones seem to crop up quite often and I have checked out threads related to this kind of issue, unfortunately I haven't been able to find a solution.

I am trying to setup a powershell script so that I can upload a file to a SFTP server automatically via the Windows Task Scheduler. When logging onto the server via the GUI everything works fine and I have used the Code Generator to generate the code for the script. I did make a few adjustments myself, though none of those are related to the credentials or any other data relevant for the connection.
When I run the script, I get an error message in powershell
Ausnahme beim Aufrufen von "Open" mit 1 Argument(en):  "Die Verbindung wurde unerwartet geschlossen. Der Server
sendete den Befehlsbeendigungsstatus 0.
Anmeldungsprotokoll (Siehe Sitzungsprotokoll für Details):
Verwende Benutzername "myusername".
Zugriff verweigert.
Anmeldung fehlgeschlagen."
In C:\ftp\ftproot\lobko\upload.ps1:23 Zeichen:5
+     $session.Open($sessionOptions)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SessionRemoteException
In an attempt to identify and fix the issue myself, I have enabled logging and noticed that according to the log, the password authentication fails (I also added the full log as an attachment). Since the code was generated from a working setup in the GUI, the password should be correct, though to be sure I double checked it. After I couldn't find any errors with the password, I did some research and found out that special characters might need to be encoded (the password includes multiple _ € and !). In order to get the encoded version of the password, I copied it from the result the GUI Code Generator gives me when I select "Script" rather than "Powershell".
This however did also not result in any improvement and after having tried to find the issue for various hours now, I figured getting help from the professionals might offer a quicker solution.

My script:
# WinSCP .NET assembly loading
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
 
# Path to last CSV file (currently just a test file)
$file = "test_" + (Get-Date -Format dd-mm-yyyy) + "_11-25.csv"
$path = (Join-Path $PSScriptRoot $file)
 
# Configure session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "sftp-server-address"
    UserName = "myusername"
    Password = "mypassword"
    SshHostKeyFingerprint = "ssh-ed25519 256 1gx2w8Rtv3wCgi7Jh8myf/KVd72cRQbow03UP8P095Q="
}
 
$session = New-Object WinSCP.Session
 
try
{
   $session.SessionLogPath = "C:\ftp\ftproot\lobko\log.txt"
    # Connect
    $session.Open($sessionOptions)
 
    # Transfer files
    $session.PutFiles($path, "/in/Bestand/*").Check()
}
finally
{
    $session.Dispose()
}
# For Debug purposes
Read-Host

If there is any information I forgot to include please let me know and I'll provide it asap.
Thank you all in advance for your help!

Best regards
Kevin

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
33,467
Location:
Prague, Czechia

Re: Unable to log onto SFTP server when using powershell script

Can you post a log file from working GUI session for a comparison?

Reply with quote

Eisman333
Joined:
Posts:
4

Re: Unable to log onto SFTP server when using powershell script

martin wrote:

Can you post a log file from working GUI session for a comparison?
Hi, thanks for your quick reply, as requested I attached a gui-log for comparison. I also did transfer the test file just to make sure that any necessary information is actually included in the log. I hope it helps with figuring out what the issue is.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
33,467
Location:
Prague, Czechia

Re: Unable to log onto SFTP server when using powershell script

I didn't identify any difference.
Double check that the hostname and username and password match. You can enable password logging to see the real password being used. Are there any special symbols in your credentials?
Also, did you check server-side log file?

Reply with quote

Eisman333
Joined:
Posts:
4

Re: Unable to log onto SFTP server when using powershell script

martin wrote:

I didn't identify any difference.
Double check that the hostname and username and password match. You can enable password logging to see the real password being used. Are there any special symbols in your credentials?
Also, did you check server-side log file?
To be sure I just double checked the hostname and username, once manually and once copy and pasting them in order to connect with a GUI Session. The manual check made no differences obvious and when I setup a GUI Session by copy pasting the credentials and hostname from my script, it also works just fine.
Unfortunately I haven't managed to figure out how to activate password logging in the script, only the line
$session.SessionLogPath = "C:\ftp\ftproot\lobko\winscp-log.txt"
in order to activate logging at all. If you could tell me what I need to add to that for password logging I'd greatly appreciate it.

As for the logs on the server, I have contacted the administrator to check the logs for the time I made a connection attempt to see if there is anything noticeable, I will let you know once I have more information on that matter.

As for my password, it uses some special characters, though I'm not sure if those are ones that need to be encoded. The "special characters" that appear in the password besides numbers and letters are _ € and !
Though as mentioned before, I have tried encoding them in the script by letting the Code generator feature encode the password for a script and copying that encoded version into my own script, with no success unfortunately.
Though to be sure that there was no mistake, I will list the different codes that appear for the various characters in case the mistake is there:
_   =   not encoded by the Code Generator
€   =   %E2%82%AC
!   =   %21

Thank you for all your help so far, I really appreciate it.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
33,467
Location:
Prague, Czechia

Re: Unable to log onto SFTP server when using powershell script

Eisman333 wrote:

Unfortunately I haven't managed to figure out how to activate password logging in the script, only the line
$session.SessionLogPath = "C:\ftp\ftproot\lobko\winscp-log.txt"
in order to activate logging at all. If you could tell me what I need to add to that for password logging I'd greatly appreciate it.
Use Logging\LogSensitive raw configuration option:
https://winscp.net/eng/docs/rawconfig

Though to be sure that there was no mistake, I will list the different codes that appear for the various characters in case the mistake is there:
_   =   not encoded by the Code Generator
€   =   %E2%82%AC
!   =   %21
What do you mean by this? These characters are not encoded by WinSCP code generation for PowerShell.

Btw, did you save your script in UTF-8 encoding?

Reply with quote

Eisman333
Joined:
Posts:
4

Re: Unable to log onto SFTP server when using powershell script

martin wrote:

Use Logging\LogSensitive raw configuration option:
https://winscp.net/eng/docs/rawconfig
Thanks for the help, I activated password logging and for my first attempt I just entered the password like normal again and it shows up properly in the log file.

martin wrote:

What do you mean by this? These characters are not encoded by WinSCP code generation for PowerShell.

Btw, did you save your script in UTF-8 encoding?
When letting the code generator create the powershell script, it didn't encode the password at all originally. Due to that when trying to figure out if maybe the special characters still need to be encoded, I checked for the encoded password by choosing a different output for the code generator though in hindsight I myself realize that was pretty stupid and probably wrong.

As for the encoding, I originally copied the code generators output into a text file which I then saved as a .ps1 file. I just checked the encoding through Notepad++ and noticed it is indeed not UTF-8.
I just changed it to UTF-8 and saved it again but now my password shows up broken in the log or rather the € are all broken. The ! and _ work just fine with the UTF-8 encoding.
In case it matters, the encoding it had before was UCS-2-LE-BOM.

Since that has the addition of BOM I also tried switching to UTF-8-BOM and with that the € signs show up properly in the log again. Incidentally, now the connection could also be established finally and the upload worked flawlessly.
Sorry for wasting your time when the solution was something so simple that I just forgot to check. All in all I haven't used WinSCP a lot but its a really nice software and made the project im working on a whole lot easier.

Thanks again for all your help and time!

Reply with quote

Advertisement

You can post new topics in this forum