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($sessionOpti ons)
# Download files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferM ode = [WinSCP.TransferMode]::Bin ary
$transferResult =
$session.GetFiles("/Backup /*.txt", "\UNC Path\Websites Backup\*", $False, $transferOptions)
# Delete External Files
$session.RemoveFiles("*.tx t")
# 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
}
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($sessionOpti
# Download files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferM
$transferResult =
$session.GetFiles("/Backup
# Delete External Files
$session.RemoveFiles("*.tx
# 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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