Archived Forum Post

Index of archived forum posts

Question:

Script to connect to sftp fails when run automatically, succeeds when run manually. (powershell)

Mar 27 '17 at 10:11

I'm using ChilkatDotNet2.dll to connect to a sftp server, the connection to the server is established but the authentication fails when it is run automatically. if we run the script manually it works.

here is the part of the script I'm having trouble with.

$sftp is a new Chilkat.SFtp object. The other variables have hardcoded values earlier in the script and contain server information and user login/pass.

    $tryCount = 0
    do {
    $successCon = $sftp.Connect($sftpHostname,$sftpPort)
    if ($successCon -eq $true){
        $successAuth = $sftp.AuthenticatePw($sftpUser,$sftpPass)
        if ($successAuth -ne $true){
            $sftp.Disconnect()
        }
    }
    $tryCount++
    }
    while(($successCon -ne $true -or $successAuth -ne $true) -and $tryCount -le 3)

As I said, there is an automated task manager that executes scripts at certain times of the day automatically, and when they are automatically run, this script fails to pass the $sftp.AuthenticatePw check, but if I run the script manually, it works. Can anyone point me in the right direction as to what might be wrong?


Answer

usually a permissions problem, make sure the scheduled job User has appropriate permissions and that the ports are accessable.


Answer

While your question is 2 years old, I am currently experiencing the same problem. I suspect the problem is that there doesn't appear to be a mechanism to accept the server key that it sends back when trying to authenticate. When using POSH-SSH, it has a parameter on the function that is AcceptKey which causes the command to accept the key provided by the server to authenticate it. I can't figure out how to do this with ChilKat. I suspect that when you are testing manually, you are using an account where something like FileZilla was used to connect to the site and when prompted for the key, you checked the box and accepted it (thereby saving it in your profile). Then when you run the script manually, it is seeing this saved key and it works. But on the automated run, the account the is running it hasn't done this previously so it is choking on the server authentication key that is being sent. If you log in as your automation account and do a manual connection with FileZilla or the like and accept and save that key, it should work as a workaround. But the proper way would be a method to do it programmatically. How did you end up solving it?