Destination empty
Hi again,
I'm still playing with the scripts provided in the help section, and I have another problem I was hoping for help with.
This is from the debug window:
For some reason the Destination comes up empty, so the script shuts down.
This is the complete script so far (haven't played around with the try/catch parts yet...)
The code
Results in the output
I'm still playing with the scripts provided in the help section, and I have another problem I was hoping for help with.
This is from the debug window:
[DBG]: PS D:\WinSCP-5.17.2-Automation>> $transferResult = $session.GetFiles($sourcePath, $destPath, $False, $transferOptions) [DBG]: PS D:\WinSCP-5.17.2-Automation>> $transferResult.Check() [DBG]: PS D:\WinSCP-5.17.2-Automation>> $transferResult Transfers --------- {/home/me/files/store/Downloaded.folder[Ident]/Downloaded.file.txt, /home/me/files/store/Another.Downloaded.Fold... [DBG]: PS D:\WinSCP-5.17.2-Automation>> $transferResult.Transfers | Sort-Object -Property @{ Expression = { (Get-Item $_.Destination).LastWriteTime } } -Descending | Select-Object -First 1 Side : Remote Destination : \\192.168.x.x\file\store\local\Downloaded.folder[Ident]\Downloaded.file.txt Touch : Chmod : Removal : FileName : /home/me/files/store/Downloaded.folder[Ident]/Downloaded.file.txt Error : [DBG]: PS D:\WinSCP-5.17.2-Automation>> Get-Item ($transferResult.Transfers | Sort-Object -Property @{ Expression = { (Get-Item $_.Destination).LastWriteTime } } -Descending | Select-Object -First 1).Destination [DBG]: PS D:\WinSCP-5.17.2-Automation>> (Get-Item ($transferResult.Transfers | Sort-Object -Property @{ Expression = { (Get-Item $_.Destination).LastWriteTime } } -Descending | Select-Object -First 1).Destination).LastWriteTime
For some reason the Destination comes up empty, so the script shuts down.
This is the complete script so far (haven't played around with the try/catch parts yet...)
param ( # Use Generate Session URL function to obtain a value for -sessionUrl parameter. $sessionUrl = "<removed>", $destPath = "X:\destination\path", $sourcePath = "/home/me/files/store/", #$listPath = "X:\destination\path\downloaded.txt", $sessionLogPath = $Null, $lastTimestamp = $Null, $timestampFile = "timestamp.dat", [Switch] $pause ) try { # Load WinSCP .NET assembly $assemblyPath = if ($env:WINSCP_PATH) { $env:WINSCP_PATH } else { $PSScriptRoot } Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll") # Setup session options from URL $sessionOptions = New-Object WinSCP.SessionOptions $sessionOptions.ParseUrl($sessionUrl) $session = New-Object WinSCP.Session Write-Host "Connecting..." $session.Open($sessionOptions) $timestampFile = "timestamp.dat" while ($True) { if (Test-Path $timestampFile) { $lastTimestamp = [DateTime]::ParseExact((Get-Content $timestampFile), 'O', $Null) } else { $lastTimestamp = $Null } Write-Host "Looking for new files..." $transferOptions = New-Object WinSCP.TransferOptions if ($lastTimestamp -ne $Null) { Write-Host "Downloading files modified after $lastTimestamp..." $transferOptions.FileMask = ("*>" + $lastTimestamp.ToString("yyyy-MM-dd HH:mm:ss") + "| *.mkv; *.avi") } else { Write-Host "Downloading all files..." } $transferResult = $session.GetFiles($sourcePath, $destPath, $False, $transferOptions) $transferResult.Check() # Find the latest downloaded file $latestTransfer = $transferResult.Transfers | Sort-Object -Property @{ Expression = { (Get-Item $_.Destination).LastWriteTime } } ` -Descending | Select-Object -First 1 if ($latestTransfer -eq $Null) { Write-Host "No files found." } else { $lastTimestamp = (Get-Item $latestTransfer.Destination).LastWriteTime Write-Host ( "Downloaded $($transferResult.Transfers.Count) files, " + "latest being $($latestTransfer.FileName) with timestamp $lastTimestamp.") } Set-Content -Path $timestampFile -Value $lastTimestamp.ToString("O") Write-Host "Waiting..." Start-Sleep -Seconds 60 } } catch { Write-Host "Error: $($_.Exception.Message)" $result = 1 } # Pause if -pause switch was used if ($pause) { Write-Host "Press any key to exit..." [System.Console]::ReadKey() | Out-Null } exit $result
The code
Write-Host ( "Downloaded $($transferResult.Transfers.Count) files, " + "latest being $($latestTransfer.FileName) with timestamp $lastTimestamp.")
Results in the output
Downloaded 2 files, latest being /home/me/files/store/Downloaded.folder[Ident]/Downloaded.file.txt with timestamp . Error: You cannot call a method on a null-valued expression.