Issue with .NET Automation Connection

Advertisement

Brendan123
Guest

Issue with .NET Automation Connection

I am currently experiencing an issue where WinSCP is able to connect to our FTP server within the GUI, but we cannot get the same results using .NET automation. When I connect and export the session code it seems to match what I have scripted, I can't use the generated code exactly as I get a syntax error I believe stemming from the fact that I am using PowerShell 5.1 and not a later version. If I connect successfully using the GUI and then export the session I get the attached output when exporting to PowerShell (password has been removed), However, when I try to run the below script, which I think is functionally equivalent, this includes the port which my export didn't but leaving it off seems to get the same, I get this error connecting:
Connection failed.
Requested action aborted: internal error in processing.
try {
   # Load WinSCP .NET assembly
   Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
 
   # Setup session options
   $sessionOptions = New-Object WinSCP.SessionOptions
   $sessionOptions.Protocol = [WinSCP.Protocol]::ftp
   $sessionOptions.FtpSecure = [WinSCP.FtpSecure]::Implicit
   $sessionOptions.HostName = 'ftp-app'
   $sessionOptions.UserName = $user
   $sessionOptions.PortNumber = 990
   $sessionOptions.Password = $password
   $sessionOptions.TlsHostCertificateFingerprint = "4a:db:1d:40:2f:ba:60:a8:ef:c7:7f:c0:5e:be:40:b9:07:21:13:fa:49:82:00:e9:b7:d9:a2:78:3e:37:f8:09"
   $session = New-Object WinSCP.Session
   
   try {
      # Open Connection
      $session.Open($sessionOptions)
 
      # Download files
      $transferOptions = New-Object WinSCP.TransferOptions
      $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
 
      $transferResult = $session.GetFiles($remoteDir + "/" + $match , $localDir + "\" , $move, $transferOptions)
 
      # Throw on any error
      $transferResult.Check()
 
      # Print results
      foreach ($transfer in $transferResult.Transfers)
      {
         writeLog "Download of $($transfer.FileName) succeeded" $log
      }
   }
   finally
   {
   # Disconnect, go home
   $session.Dispose()
   }
   writeLog "Processing Complete." $log
   exit 0
}
catch [Exception] {
   writeLog $_.Exception.Message $log
   exit 1
}
The user and password end up set the same, I'm wondering if it is related to the certificate in some way as at times I do get prompts using the GUI that I it does not trust the full chain of the TLS certificate for FTPS.

Reply with quote

Advertisement

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

Re: Issue with .NET Automation Connection

What is the problem with generated code in PowerShell 5.1? Can you please give us more details about this problem?

Regarding your connection problem, please post session log files both from the code and the GUI.

Reply with quote

Brendan123
Guest

RE: Issue with .NET Automation Connection

Hello Martin, I actually was not previously aware of the session log for WinSCP but after your reply I found the details on it and I may be able to use this to resolve the issue which may be to do with the FTP server. I can re-open this topic if needed and include session logging. But for now it is currently resolved by reverting a version upgrade on the FTP server software.

Additionally for the convert to PowerShell I had quickly misread an error about not correctly locating the winscpnet.dll file and I in fact do not get a syntax error from the generated script once pointed to the correct DLL location. Thank you for pointing me to the SessionLogPath and DebugLogPath properties.

Reply with quote

Advertisement

You can post new topics in this forum