$srcpath = 'F:\HSBC\TO HSBC\*.pgp'
$filepath = Get-ChildItem $srcpath -File | Where-Object { $_.Extension -ne '.filepart' } # Exclude .filepart files
$tarpath = '/' # Change the tarpath to a single forward slash
$compath = 'F:\BackupSFTP\'
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$SessionOptions = New-Object WinSCP.SessionOptions
$SessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$SessionOptions.Hostname = "############"
$SessionOptions.Username = "######"
$SessionOptions.PortNumber = "10022"
$SessionOptions.PrivateKeyPassphrase = "######"
$SessionOptions.SshPrivateKeyPath = "##################"
# Set the SSH host key fingerprint
$SessionOptions.SshHostKeyFingerprint = "######################"
$session = New-Object WinSCP.Session
try {
$session.Open($SessionOptions)
foreach ($file in $filepath) {
$transferResult = $session.PutFiles($file.FullName, $tarpath)
foreach ($transfer in $transferResult.Transfers) {
if ($transfer.Error -eq $null) {
Write-Host "Upload of $($transfer.FileName) Succeeded"
} else {
Write-Host "Upload of $($transfer.FileName) Failed: $($transfer.Error.Message)"
}
}
}
} finally {
$session.Dispose()
}