PowerShell Error Transferring File - PutFiles?

Advertisement

lkubler
Joined:
Posts:
8
Location:
Wisconsin

PowerShell Error Transferring File - PutFiles?

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?)
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!

Reply with quote

Advertisement

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

Re: PowerShell Error Transferring File - PutFiles?

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.

Reply with quote

lkubler
Joined:
Posts:
8
Location:
Wisconsin

Re: PowerShell Error Transferring File - PutFiles?

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:
$transferResult = $session.PutFiles("C:\Scripts\MyFile.CSV", $remotePath + "/*.*", $false, $transferOptions)
And
$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

Reply with quote

Advertisement

john_roa
Guest

Why didn't the library throw an error?

This article helped me troubleshoot an issue I was having where the PutFiles method was firing but the file I was trying to push wasn't landing on the site I was pushing to.

My question, though, is why did the error (in my case I was trying to push to a directory that didn't exist on the target site) only show up in the log? Why didn't the library throw the error? It would have saved me a lot of time if it had thrown the error rather than just eating it and having to search your logs to see that an error happened.

Reply with quote

Advertisement

You can post new topics in this forum