troubleshooting Question

Help with WinSCP Powershell script

Avatar of Simon Walton
Simon Walton asked on
Assembly.NET ProgrammingScripting LanguagesPowershell
2 Comments1 Solution154 ViewsLast Modified:
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
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros