PowerShell - sFTP files less than 10 minutes old to remote directory not working; syncs all files
I am trying to only copy files 10 minutes old or less but the synch keeps grabbing all the files. What am I doing wrong? I don't see any errors returned nor in the log.
#$ErrorActionPreference = "SilentlyContinue"
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
#Current Date-Time minus:
$Age = (Get-Date).AddMinutes(-10)
$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
ForEach ($Folder in $listOfFolders){
$newFiles = Get-ChildItem -Recurse $Folder | Where-Object { $_.LastWriteTime -ge $Age}
$newFiles | Select Name, DirectoryName, LastWriteTime
}
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "FileServer"
UserName = "User"
Password = "Password"
GiveUpSecurityAndAcceptAnySshHostKey = $true
}
$session = New-Object WinSCP.Session
$session.SessionLogPath = "C:\Temp\sFTP.log"
$session.Open($sessionOptions)
# Synchronize files
ForEach ($file in $newFiles){
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*>=$Age"
$newFolder = $file.DirectoryName
$destFolder = $newFolder.Trim("F:\")
$synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Remote, $file.DirectoryName, "/home/egdms_user/Miami/" + $destFolder -replace ("\\", "/"), $False, $transferOptions)
# Throw on any error
$synchronizationResult.Check()
}
# Disconnect, clean up
$session.Dispose()
#$ErrorActionPreference = "SilentlyContinue"
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
#Current Date-Time minus:
$Age = (Get-Date).AddMinutes(-10)
$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
ForEach ($Folder in $listOfFolders){
$newFiles = Get-ChildItem -Recurse $Folder | Where-Object { $_.LastWriteTime -ge $Age}
$newFiles | Select Name, DirectoryName, LastWriteTime
}
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "FileServer"
UserName = "User"
Password = "Password"
GiveUpSecurityAndAcceptAnySshHostKey = $true
}
$session = New-Object WinSCP.Session
$session.SessionLogPath = "C:\Temp\sFTP.log"
$session.Open($sessionOptions)
# Synchronize files
ForEach ($file in $newFiles){
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*>=$Age"
$newFolder = $file.DirectoryName
$destFolder = $newFolder.Trim("F:\")
$synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Remote, $file.DirectoryName, "/home/egdms_user/Miami/" + $destFolder -replace ("\\", "/"), $False, $transferOptions)
# Throw on any error
$synchronizationResult.Check()
}
# Disconnect, clean up
$session.Dispose()