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
}
Powershell.NET ProgrammingAssemblyScripting Languages

Avatar of undefined
Last Comment
Simon Walton

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Simon Walton

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
Your help has saved me hundreds of hours of internet surfing.
fblack61