Script Task in SSIS failing to connect to SFTP using WinSCP

28 Views Asked by At

I used script task using c# code to do an if else logic to determine the existence of the file on the remote system before pulling the file out.

At first I created the c# code to run on its own to make sure that the code I had was working properly and it was working properly.

From this I thought if I can transfer this over to the script task in ssis then theoretically it should work exactly the same. However it keeps giving me failure of exception and fails the code.

I am not sure why it keeps doing this especially since the code work if I just run it as c# on its own.

I was following this site as a guide https://winscp.net/eng/docs/library_ssis

And I also have the WinSCPnet.dll added in the references so its should be able to read the winscp commands for c#.

I don't know what is wrong please help.

This is the code.

public void Main()
        {

            string Log = Dts.Variables["User::MLog"].Value.ToString();
            string SFTPOne = Dts.Variables["User::SFTPFileOne"].Value.ToString();
            string SFTPTwo = Dts.Variables["User::SFTPFileTwo"].Value.ToString();
            string Src = Dts.Variables["User::SrcPath"].Value.ToString();

            
            SessionOptions sessionsOption = new SessionOptions
            {

                Protocol = Protocol.Sftp,
                HostName = Dts.Variables["User::SFTPHostName"].Value.ToString(),
                UserName = Dts.Variables["User::SFTPUsername"].Value.ToString(),
                Password = Dts.Variables["User::SFTPPassword"].Value.ToString(),
                PortNumber = 22,
                SshHostKeyFingerprint = Dts.Variables["User::SFTPSshHostKeyFingerprint"].Value.ToString(),
                
            };
            
            try
            {
                using (Session session = new Session())
                {

                    session.SessionLogPath = Log;

                    session.Open(sessionsOption);

                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;

                    if (session.FileExists(SFTPOne))
                    {
                        RemovalOperationResult removalOperation;
                        removalOperation = session.RemoveFiles(SFTPOne);
                        TransferOperationResult transferResult;
                        transferResult = session.GetFiles(SFTPTwo, Src, true, transferOptions);
                    }
                    else
                    {
                        MessageBox.Show("file doesn't exist");
                    }
                }
            }
            catch (Exception e)
            {
                Dts.TaskResult = (int)DTSExecResult.Failure;
            }
            
            Dts.TaskResult = (int)ScriptResults.Success;
        }
0

There are 0 best solutions below