Link to home
Start Free TrialLog in
Avatar of Simon Walton
Simon Walton

asked on

Help with WinSCP Powershell script

Hi All

We have a Powershell winSCP script to transfer Website Backup Dir to Domain UNC folder and  we want to delete the Website Backup files after the transfer.

Looking at the WinSCP tutorials we understood the following would work (What is wrong regarding the delete part)

see code

try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCP\WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "hostname"
        UserName = "username"
        Password = "password"
        SshHostKeyFingerprint = "ssh-ed................................................="
    }
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        # Download files
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
 
        $transferResult =
            $session.GetFiles("/Backup/*.txt", "\UNC Path\Websites Backup\*", $False, $transferOptions)

      # Delete External Files
        $session.RemoveFiles("*.txt")

 
        # Throw on any error
        $transferResult.Check()
 
        # Print results
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host "Download of $($transfer.FileName) succeeded"
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Simon Walton
Simon Walton

ASKER

The original error showed nothing removed.

changing the file path to "/Backup/*.txt" fixed the issue but we are sure we did that originally.

Its fixed and that's the main thing

Almost feel a little stupid ;)

Thank you