We help IT Professionals succeed at work.

Help with WinSCP Powershell script

100 Views
Last Modified: 2020-04-08
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
}
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Simon WaltonIT Manager

Author

Commented:
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
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.