SOLVED!
Got it sorted. Seems like Put was the only way to get some of my logic to work. Thanks again for your help with the mask.
Final code:
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "FileServer"
UserName = "User"
Password = "PASSWORD"
GiveUpSecurityAndAcceptAnySshHostKey = $true
}
$Folder_EGDMS_DIU = "F:\EGDMS_DIU"
$Folder_EGDMS_LOGS = "F:\EGDMS_LOGS"
$Folder_EGDMS_PROVISION = "F:\EGDMS_PROVISION"
$Folder_EGDMS_RAW = "F:\EGDMS_RAW"
$Folder_EGDMS_REPORTS = "F:\EGDMS_REPORTS"
$listOfFolders = $Folder_EGDMS_DIU, $Folder_EGDMS_LOGS, $Folder_EGDMS_PROVISION, $Folder_EGDMS_RAW, $Folder_EGDMS_REPORTS
$session = New-Object WinSCP.Session
$session.SessionLogPath = "C:\Temp\sFTP.log"
$session.Open($sessionOptions)
# Synchronize files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*>=10N"
ForEach ($Folder in $listOfFolders){
# If the selected item is file, find all contained files and folders recursively
If (Test-Path $Folder -PathType container){
$files =
@($Folder) +
(Get-ChildItem $Folder -Recurse | Select-Object -ExpandProperty FullName)}
Else{
$files = $Folder}
$parentLocalPath = Split-Path -Parent (Resolve-Path $Folder)
foreach ($localFilePath in $files){
$remoteFilePath =
$session.TranslateLocalPathToRemote(
$localFilePath, $parentLocalPath, $remotePath)
$fullRemotePath = "/home/user/Miami/" + $remoteFilePath
if (Test-Path $localFilePath -PathType container){
# Create remote subdirectory, if it does not exist yet
if (!($session.FileExists("/home/user/Miami/" + $remoteFilePath))){
$session.CreateDirectory("/home/user/Miami/" + $remoteFilePath)}}
Else{
Write-Host "Moving file $localFilePath to $fullRemotePath..."
# Upload file and remove original
$session.PutFiles($localFilePath, $fullRemotePath, $False, $transferOptions).Check()
}
}
}
# Disconnect, clean up
$session.Dispose()
Final code:
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "FileServer"
UserName = "User"
Password = "PASSWORD"
GiveUpSecurityAndAcceptAnySshHostKey = $true
}
$Folder_EGDMS_DIU = "F:\EGDMS_DIU"
$Folder_EGDMS_LOGS = "F:\EGDMS_LOGS"
$Folder_EGDMS_PROVISION = "F:\EGDMS_PROVISION"
$Folder_EGDMS_RAW = "F:\EGDMS_RAW"
$Folder_EGDMS_REPORTS = "F:\EGDMS_REPORTS"
$listOfFolders = $Folder_EGDMS_DIU, $Folder_EGDMS_LOGS, $Folder_EGDMS_PROVISION, $Folder_EGDMS_RAW, $Folder_EGDMS_REPORTS
$session = New-Object WinSCP.Session
$session.SessionLogPath = "C:\Temp\sFTP.log"
$session.Open($sessionOptions)
# Synchronize files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*>=10N"
ForEach ($Folder in $listOfFolders){
# If the selected item is file, find all contained files and folders recursively
If (Test-Path $Folder -PathType container){
$files =
@($Folder) +
(Get-ChildItem $Folder -Recurse | Select-Object -ExpandProperty FullName)}
Else{
$files = $Folder}
$parentLocalPath = Split-Path -Parent (Resolve-Path $Folder)
foreach ($localFilePath in $files){
$remoteFilePath =
$session.TranslateLocalPathToRemote(
$localFilePath, $parentLocalPath, $remotePath)
$fullRemotePath = "/home/user/Miami/" + $remoteFilePath
if (Test-Path $localFilePath -PathType container){
# Create remote subdirectory, if it does not exist yet
if (!($session.FileExists("/home/user/Miami/" + $remoteFilePath))){
$session.CreateDirectory("/home/user/Miami/" + $remoteFilePath)}}
Else{
Write-Host "Moving file $localFilePath to $fullRemotePath..."
# Upload file and remove original
$session.PutFiles($localFilePath, $fullRemotePath, $False, $transferOptions).Check()
}
}
}
# Disconnect, clean up
$session.Dispose()