Archiving on the remote site during download

Advertisement

Vann_the_Red
Joined:
Posts:
27
Location:
NC, USA

Archiving on the remote site during download

Good afternoon, all! I was adapting the script provided at: Moving local files to different location after successful upload
to download from a remote site and then archive to a different directory on that remote site. I'm uncertain, though, where the Session.MoveFiles call should live. Any advice would be greatly appreciated. Code below.

Regards,
VtR

param (
    $localPath = "C:\upload\*",
    $remotePath = "/home/user/",
    $backupPath = "/home/archive/"
)
 
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "example.com"
        UserName = "user"
        Password = "mypassword"
        SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
    }
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        # Download files, collect results
        $transferResult = $session.GetFiles($remotePath, $localPath)
 
        # Iterate over every transfer
        foreach ($transfer in $transferResult.Transfers)
        {
            # Success or error?
            if ($transfer.Error -eq $Null)
            {
                Write-Host "Download of $($transfer.FileName) succeeded, moving to backup"
                # Download succeeded, move source file to backup
                # Move-Item $transfer.FileName $backupPath
            $transferResult = $session.MoveFiles($remotePath, $backupPath)
            }
            else
            {
                Write-Host "Download of $($transfer.FileName) failed: $($transfer.Error.Message)"
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

Reply with quote

Advertisement

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

Re: Archiving on the remote site during download

Your placement is correct (after all, it's the same what the original code does).
But it's Session.MoveFile, not Session.MoveFiles. And you need to use $transfer.FileName. And Session.MoveFile does not return anything.
$session.MoveFiles($transfer.FileName, $backupPath)
See also

Reply with quote

Advertisement

You can post new topics in this forum