Topic "PowerShell Error Transferring File - PutFiles?"

Author Message
lkubler
[View user's profile]

Joined: 2015-02-18
Posts: 7
Location: Wisconsin
Hi,

New to PowerShell and WinSCP but trying to setup a script to upload files to an FTP server. I can connect and read the remote folder contents but when I try to send a file with PutFiles method I get a rather generic error. "Error transferring file"

Is there any way to get more detail as to what exactly the error is? Like I said I can read the file names of the remote folder using ListDirectory method so I'm sure I'm connecting with the FTP server all right. Just can't figure out why the transfer is failing.

Here's my code: (Maybe I'm missing something?)

Code:

try {
   # Load WinSCP .NET assembly
   Add-Type -Path "C:\Scripts\WinSCPnet.dll"
   
   # Setup variables
   $localPath = "C:\Scripts"
   $logFilePath = "C:\Scripts\Log Files"
   $remotePath = "/Orders"
   
   # Setup session options
   $sessionOptions = New-Object winscp.sessionoptions
   $sessionOptions.protocol = [WinSCP.Protocol]::ftp
   $sessionOptions.HostName = "ftp_server"
   $sessionOptions.username = "ftp_user"
   $sessionOptions.password = "password"
   
   $session = New-Object WinSCP.Session

   # Clear the host screen.
   Write-Host | Clear-Host
   
   try
   {
      # Connect
      $session.Open($sessionOptions)
      
      # Upload files.
      $transferOptions = New-Object winscp.TransferOptions
      $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
            
      $transferResult = $session.PutFiles("C:\Scripts\MyFile.CSV", "Orders", $false, $transferOptions)
            
      # Thow an error.
      $transferResult.Check()
            
      # Print results of transfer.
      foreach ($transfer in $transferResult.Transfers)
      {
         Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
      }
      
      # No new orders to send or finished sending.
      # Check remote site for return files.
      $directory = $session.ListDirectory($remotePath)
      
      # Select files matching wildcard.
      $remoteFiles = $directory.Files | Where-Object { $_.Name -like "*.csv" }
      
      if ($remoteFiles)
      {
      # Download each return file.
         foreach ($file in $remoteFiles)
         {
            Write-Host ("{0} with size {1} and last modification at {2}`n" -f
               $file.Name, $file.length, $file.LastWriteTime)
         }
      }
   }
   finally
   {
      # Disconnect, clean up
      $session.Dispose()
   }
   Write-Host "Done, no errors!"
   exit 0
}
catch [Exception]
{
   Write-Host $_.Exception.Message
   Write-Host "Complete with Error!"
   exit 1
}


Any help is greatly appreciated.

Thanks in advance,
Linn

Update: I was able to figure out how to use the $TransferResult.Failures to see more details and it says access denied on the remote folder. But I can use WinSCP on the same computer to manually transfer files to that folder with the same user credentials. Any thoughts about the rights differences?

Thanks!
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

A log from WinSCP GUI for comparison would be helpful too.
_________________
Martin Prikryl
lkubler
[View user's profile]

Joined: 2015-02-18
Posts: 7
Location: Wisconsin
prikryl wrote:
Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

A log from WinSCP GUI for comparison would be helpful too.


Hi Martin,

Attached are the log files, I believe I sufficiently sanitized them.

Thanks,
Linn
ftp_user@ftp_server.log (2.86 KB) [Download]

Description: WinSCP log

20150309.log (6.42 KB) [Download]

Description: Script log

lkubler
[View user's profile]

Joined: 2015-02-18
Posts: 7
Location: Wisconsin
Ah Ha! Got it! Problem seems to be that in the PutFiles method I need to include *.* on the destination path. So I changed the following lines of code from above:

Code:

$transferResult = $session.PutFiles("C:\Scripts\MyFile.CSV", $remotePath + "/*.*", $false, $transferOptions)


And

Code:

$directory = $session.ListDirectory($remotePath + "/Outgoing")


To match the location of the files I need to download after the upload. I'll work that bit out next.

Thanks for the offer of help, Martin.
Linn
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
Thanks for posting your solution.

lkubler wrote:
Ah Ha! Got it! Problem seems to be that in the PutFiles method I need to include *.* on the destination path.

Well, you do not. But you need to include the trailing slash.
See https://winscp.net/eng/docs/library_session_putfiles
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License