WinSCPnet / PowerShell $session.RemoveFiles() not working

Advertisement

RVerbrugge
Joined:
Posts:
1

WinSCPnet / PowerShell $session.RemoveFiles() not working

Hello,
I have been searching through this forum and online but can't seem to find what is wrong.
Using the PowerShell code below, I can connect to the server, the files get copied but the do not get removed even though even the logfile says they are and the script output says:
Download of /---SRC---/testdir/blablabla-with reports-XXXXX-YYYYY.txt succeeded, removing from source
Removing of file /---SRC---/testdir/blablabla-with reports-XXXXX-YYYYY.txt succeeded
Please note the source system is running on AIX 7.1 and deleting the files using WinSCP GUI works.

Thanks for your time!
param (
    $localPath = "\\---\Test",
    $remotePath = "/---/testdir"
)
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off
$transferOptions.FileMask = "*.*|.no_delete,*.part,*.tmp"
 
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
 
    # Setup session options
    $t = Get-StoredCredential QCPR
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "---"
        UserName = $t.UserName
        Password = $t.GetNetworkCredential().password
        SshHostKeyFingerprint = "ecdsa-sha2-nistp256 256 vYoOOSOfaocnbbBjN78+IBvKQN2SSO3fpctvkvVM4u4"
    }
 
    $session = New-Object WinSCP.Session
    $session.SessionLogPath = "c:\temp\scplog.log"
 
    try
    {
        $session.Open($sessionOptions)
        $synchronizationResult = $session.SynchronizeDirectories(
            [WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $False)
 
        # Deliberately not calling $synchronizationResult.Check
        # as that would abort our script on any error.
        # We will find any error in the loop below
        # (note that $synchronizationResult.Downloads is the only operation
        # collection of SynchronizationResult that can contain any items,
        # as we are not removing nor uploading anything)
 
        # Iterate over every download
        foreach ($download in $synchronizationResult.Downloads)
        {
            # Success or error?
            if ($download.Error -eq $Null)
            {
                Write-Host "Download of $($download.FileName) succeeded, removing from source"
                # Download succeeded, remove file from source
                $filename = [WinSCP.RemotePath]::EscapeFileMask($download.FileName)
                $removalResult = $session.RemoveFiles($filename)
 
                if ($removalResult.IsSuccess)
                {
                    Write-Host "Removing of file $($download.FileName) succeeded"
                }
                else
                {
                    Write-Host "Removing of file $($download.FileName) failed"
                }
            }
            else
            {
                Write-Host (
                    "Download of $($download.FileName) failed: $($download.Error.Message)")
            }
        }
    }
    finally
    {
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

Reply with quote

Advertisement

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

Re: WinSCPnet / PowerShell $session.RemoveFiles() not working

Well, WinSCP sent remove request to the server, and the server acknowledged it. If the file was not deleted, it is probably problem on the server side.
Can you delete the files in WinSCP GUI? Or in any other SFTP client?

Reply with quote

Advertisement

You can post new topics in this forum