Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: Authentication Failed

So it seems that the authentication code matches your manual login. Then it's most likely an incorrect password. Note that in PowerShell, certain characters in string literals have special meaning and need to be escaped. Notably the $ sign.
WinSCP GUI can generate a working code template for you, with all necessary escaping:
https://winscp.net/eng/docs/ui_generateurl#code
crughp

POSFTP is the one where I did it manually.
crughp

Here is what you requested from me. Scripted Magic is the scripted interaction.
martin

Because you have removed this code from line #19:
$session.Open($sessionOptions)
crughp

I feel like I did not get an authentication failure this time. This is what I got.
martin

It's the line #3 that causes the problem (not the line #15). Remove #3, keep #15.
crughp

This is the whole script I am running without the security information.
crughp

I just ran the exact code that you see above in Powershell ISE. This one is one where I ran the script with the command outside of the curly brackets.
crughp

I just ran the exact code that you see above in Powershell ISE. This one is one where I ran the script with the command inside of the curly brackets.
martin

The wording of the error message implies you still tried the Session.SessionLogPath, not the $session.SessionLogPath (despite your screenshot showing otherwise). Please make sure to use the $session.SessionLogPath. The Session is the class name, while the $session is the variable name in your code.
crughp

I am attaching a screen shot where with arrows to where I tried to put the command as well here.
crughp

PS C:\Windows\system32> C:\it.info\winscp.ps1
Session.SessionLogPath : The term 'Session.SessionLogPath' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\it.info\winscp.ps1:3 char:1
+ Session.SessionLogPath = "C:\it.info\script.log"
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Session.SessionLogPath:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Exception calling "Open" with "1" argument(s): "Network error: Connection to "bitbus.ttuhsc.edu" timed out."
At C:\it.info\winscp.ps1:19 char:5
+ $session.Open($sessionOptions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SessionRemoteException
martin

Before $session.Open, put:
$session.SessionLogPath = "C:\some\path\winscp.log"
crughp

I seem to be getting an error on the script. I am not sure of the syntax and where to add that into the script. I tried a couple of different places and got this error.
Session.SessionLogPath : The term 'Session.SessionLogPath' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\it.info\winscp.ps1:3 char:1
+ Session.SessionLogPath = "C:\it.info\script.log"
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Session.SessionLogPath:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Exception calling "Open" with "1" argument(s): "Authentication failed.
Authentication log (see session log for details):
Using username "montgomerycnty_sftp".
Access denied.
Authentication failed."
At C:\it.info\winscp.ps1:18 char:5
+ $session.Open($sessionOptions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SessionRemoteException
martin

Re: Authentication Failed

To generate the session log file with .NET assembly, set Session.SessionLogPath.
crughp

Re: Authentication Failed

The logs are easy to get from the manual login, but I am not sure where they populate from the scripted task. Can you shed some light on that for me please?
martin

Re: Authentication Failed

It's definitely not the SshHostKeyFingerprint.
Please post session log files both from the code and from successful "manual" login with the same credentials.
Guest

Authentication Failed

I am using this:
# Path to WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
 
# Session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "blah.com"
    UserName = "XXXXX"
    Password = "XXXXX"
    SshHostKeyFingerprint = "ecdsa-sha2-nistp256 256 xx:xx:xx etc"
}
 
$session = New-Object WinSCP.Session
 
try {
    # Open SFTP session
    $session.Open($sessionOptions)
 
    # Upload file (local -> remote)
    $transferResult = $session.PutFiles(
        "C:\doz\", # local path (can be wildcard)
        "/New/" # remote folder or file path
    )
 
    # Throw if any transfer failed
    $transferResult.Check()
}
finally {
    $session.Dispose()
}

The error I am getting is:

Exception calling "Open" with "1" argument(s): "Authentication failed.
Authentication log (see session log for details):
Using username "XXXXX".
Access denied.
Authentication failed."
At C:\it.info\winscp.ps1:17 char:5
+ $session.Open($sessionOptions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SessionRemoteException

What I am trying to figure out is that where is the authentication failing in the process. I know the credentials are good because I can manually use them, so I have to assume it has something to do with the SshHostKeyFingerprint, so I think I need to know which key should I be using in the screen shot.