Can upload files in GUI but not PowerShell using WinSCP

Advertisement

kul2bme
Joined:
Posts:
7

Can upload files in GUI but not PowerShell using WinSCP

I have successfully (finally!) gotten large files to upload to an SFTP site with the GUI. I tried getting the setup identical in the PowerShell script using WinSCP dll. However, only the small files work in the PS script process. The large files still give me an error in the PS script process. It still appears to creating a temporary file for large file transfers even though I believe I have it set properly to not do so. See below ...
Error: Exception calling "Check" with "0" argument(s): "Cannot overwrite remote file '/REMOTEPATH/70d0ff59-d4a7-4648-b106-4e36ef4f550d.txt'.$$

Press 'Delete' to delete the file and create new one instead of overwriting it.$$ Permission denied.
Error code: 3
Error message from server (en): Permission denied"
I am attaching the log file from the GUI where the files worked as well as the log file from when the PS script had all the same settings, as best that I can tell, but failed for the large file. I have edited the files for privacy. Please compare and let me know what I am doing wrong.

Additionally, I am including below the code from the PS script for review, also edited for privacy.
################ Send to SFTP ########################
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll";
 
    #pass the new file to secure FTP
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "HOST NAME"
        UserName = "USERNAME" 
        Password = 'PASSWORD' 
        SshHostKeyFingerprint = "ssh-dss 1024 FINGER PRINT"
        Timeout = 6000
    }
 
    $session = New-Object WinSCP.Session;
    try
    {
        $session.SessionLogPath = '\\NGN-IMAGE\NCTracks\Logs\winscp.log';
        
        ## Connect
        $session.Open($sessionOptions);
        
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.AddRawSettings("NewerOnly", "0");
        $transferOptions.AddRawSettings("PreserveTimeDirs", "0");
        $transferOptions.AddRawSettings("ExcludeHiddenFiles", "0");
        $transferOptions.AddRawSettings("ExcludeEmptyDirectories", "0");
        $transferOptions.AddRawSettings("EncryptNewFiles", "1");
        $transferOptions.AddRawSettings("SendBuf", "0");
 
        $transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off
        $transferOptions.FilePermissions = $Null # This is default
        $transferOptions.PreserveTimestamp = $False
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
        $transferOptions.OverwriteMode = [WinSCP.OverwriteMode]::Overwrite
        
        ## copy file to SFTP
        $allFiles = -join($filepath,"*")
        $transferResult = $session.PutFiles($allFiles,$remotePath,$False,$transferOptions);
        $transferResult.Check();
        
        ##close connection
        $Session.Close()
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose();
    }
 
    $FinishedMailParams= @{
        To = 'ToEmail'
        From = 'FromEmail'
        Port = ''
        SmtpServer = 'SMTPServer'
        Subject = 'SUBJECT'
        Body = "EMAIL BODY"
    }
 
    Send-MailMessage @FinishedMailParams;
} 
catch
{
    ## send email
    $ErrorMailParams = @{
        To = 'ToEmail'
        From = 'FromEmail'
        Port = ''
        SmtpServer = 'SMTPServer'
        Subject = 'SUBJECT'
        Body = "Error: $($_.Exception.Message)"
    }
 
    Send-MailMessage @ErrorMailParams;
}

Reply with quote

Advertisement

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

Re: Can upload files in GUI but not PowerShell using WinSCP

Was the fix disabling the "Optimize connection buffer size"? The SendBuf is raw session setting, not transfer setting. So it should go to SessionOptions:
$sessionOptions.AddRawSettings("SendBuf", "0");
The rest of your AddRawSettings calls can probably be removed (those are defaults or irrelevant).

Reply with quote

kul2bme

Re: Can upload files in GUI but not PowerShell using WinSCP

I changed SendBuf and removed remaining AddRawSettings and was able to transfer both large and small files perfectly. Thank you Martin for your help!

Reply with quote

kul2bme
Joined:
Posts:
7

Re: Can upload files in GUI but not PowerShell using WinSCP

Well I spoke too soon. The error was delayed in getting back to my email. It still gave me the error on the large file...
Error: Exception calling "Check" with "0" argument(s): "Cannot overwrite remote file '/REMOTEPATH/af2e8808-9242-4897-ab39-52025b856913.txt'.$$

Press 'Delete' to delete the file and create new one instead of overwriting it.$$ Permission denied.
Error code: 3
Error message from server (en): Permission denied"
I thought the parameter
$transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off
was supposed to take care of trying to create a temp file on the destination.

I have attached the log edited for privacy.

Reply with quote

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

Re: Can upload files in GUI but not PowerShell using WinSCP

I do not see any temp file being created in the log. Am I missing something?
Are you able to upload the very same file anyhow? In WinSCP GUI? Or in any other SFTP client? Post a log file for that.
And please post new PowerShell log file with timeout set correctly. Currently you have it set to 0. The Timeout is TimeSpan, not an integer. You might use TimeoutInMilliseconds instead.

Reply with quote

Advertisement

You can post new topics in this forum