FTP upload script only uploading single file from directory at a time. Want all the files to go up
Hi All I have this script and it works but only upload 1 file at a time on a 5min task schedule. I want it to upload all files in the directory every 5 min on a task schedule.
Not sure where I have gone wrong. Any help is appreciated.
Not sure where I have gone wrong. Any help is appreciated.
param ( $localPath = "C:\WinDSX\output\*", $remotePath = "/from-dsx/", $backupPath = "C:\WinDSX\Uploaded files", $logPath = "C:\WinDSX\log.txt" ) try { # Load WinSCP .NET assembly Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll" # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp HostName = "somedomain.com" UserName = "someusername" # Password = "mypassword" SshHostKeyFingerprint = "somehash" SshPrivateKeyPath = "someprivatekey" } $session = New-Object WinSCP.Session try { # Connect $session.Open($sessionOptions) # Upload files, collect results $transferResult = $session.PutFiles($localPath, $remotePath) # Iterate over every transfer foreach ($transfer in $transferResult.Transfers) { # Success or error? if ($transfer.Error -eq $Null) { $logMessage = "Upload of $($transfer.FileName) succeeded, moving to backup" Write-Host $logMessage Out-File -FilePath $logPath -Append -InputObject $logMessage # Upload succeeded, move source file to backup Move-Item $transfer.FileName $backupPath } else { $logMessage = "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)" Write-Host $logMessage Out-File -FilePath $logPath -Append -InputObject $logMessage } } # Delete all files in the output directory except the log file # Get-ChildItem "C:\WinDSX\output" -Exclude (Split-Path -Leaf $logPath) | Remove-Item -Force } finally { # Disconnect, clean up $session.Dispose() } exit 0 } catch { $errorMessage = "Error: $($_.Exception.Message)" Write-Host $errorMessage Out-File -FilePath $logPath -Append -InputObject $errorMessage exit 1 }