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
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:
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
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
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