WinSCP automation using PowerShell

Advertisement

Guest

WinSCP automation using PowerShell

I am trying to automate a process which involves copying a file from a Windows share to a Red Hat Linux 7.2 server. I am using PowerShell for this and attached is the script (Automation-CSV-FTP-Moodle-Emrollments QA). When I run the script it is breaking at the open session with error. The same script executes ok the QA server. I checked that firewall is dead. No iptables.
Any ideas please. I am running this scrip on my machine and I can connect manually using winscp to the same server with no issues.
Exception calling "Open" with "1" argument(s): "Connection has been unexpectedly closed. Server sent command exit status 0.
Authentication log (see session log for details):
Using username "root".
Authentication failed."
At D:\Automation-CSV-FTP-Moodle-Emrollments LMS.ps1:28 char:1
+ $session.Open($sessionOptions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SessionRemoteException
 
Exception calling "PutFiles" with "4" argument(s): "Session is not opened"
At D:\Automation-CSV-FTP-Moodle-Emrollments LMS.ps1:38 char:1
+ $session.PutFiles("$SourceSuspendOldPath", "$DestinationSuspendOldPat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException
 
Exception calling "PutFiles" with "4" argument(s): "Session is not opened"
At D:\Automation-CSV-FTP-Moodle-Emrollments LMS.ps1:43 char:1
+ $session.PutFiles("$SourceTodayPath", "$DestinationTodayPath", $False ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException
error log : tail -f /var/log/secure
Jun 29 11:32:01 xxxxxxx sshd[23287]: error: Received disconnect from xxxxxxxxxx: 13: Unable to authenticate [preauth]

Reply with quote

Advertisement

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

Re: winscp automation using powershell

Your code does not seem to match the log file. In the log file, I cannot see any password being passed in.

When I run your code I can clearly see the password being used in the log file:
. 2017-06-30 16:23:56.362 User name: XXX (Password: Yes, Key file: No, Passphrase: No)
> 2017-06-30 16:23:56.362 Script: open sftp://XXX:***@XXX -hostkey="..." -timeout=15
While your logs says:
. 2017-06-29 11:43:37.509 User name: root (Password: No, Key file: No, Passphrase: No)
> 2017-06-29 11:43:37.509 Script: open sftp://root@xxx.xxx.xx.xxx -hostkey="..." -timeout=15
Something does not match.

Reply with quote

grkraj
Joined:
Posts:
1

Automation using power shell

Martin, Thank you for replying
You are right, It is odd.
In the code I changed user name to different admin account (instead of root) and it works. You can see in the attached log that it is passing the password.
What is going on here?. Why is it not working as root

Please ignore that it is failing at the end because it cannot find the files.

Reply with quote

martin
Site Admin
martin avatar

Re: Automation using power shell

Do you mean that you have only changed root to winscpuser? No other change in the code was done? Not even the password?

Reply with quote

Guest

winscp automation using powershell

I had the same issue. Found out that WinSCP does not like control characters at the end of the password so I changed the password and it worked.

Reply with quote

Advertisement

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

Re: winscp automation using powershell

Anonymous wrote:

I had the same issue. Found out that WinSCP does not like control characters at the end of the password so I changed the password and it worked.
WinSCP should have no problem with any character in a password.
Isn't it rather that your password contained characters that have a special meaning in PowerShell? Like a dollar sign or a backtick?

Reply with quote

or9
Guest

Found the Solution to this error

What is most likely the case is one of the password characters contains an % which is an escape key in batch so let's say we want to send a $ through PowerShell.

The sent text would be XXXXX%%24 of which PowerShell receives XXXXX%24 -> XXXXX$

Reply with quote

Advertisement

You can post new topics in this forum