hi,
Newbie to WinSCP and would like your help. I follow this sample script here:
https://winscp.net/eng/docs/script_upload_most_recent_file to upload the latest file from a local drive to a remote host. I set this script to run with Task Scheduler, and the scheduler runs successful, but no new file ever been copied from my local drive to a remote host. Here's my script:
try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "varHName"
UserName = "varUName"
Password = "varPwd"
SshHostKeyFingerprint = "ssh-dss 1024 xxxxxxxx...."
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
$localPath = "c:\users\ren\dir\"
$remotePath = "/myHome/Dng/ht/"
# Get list of files in the directory
# $directoryInfo = $session.ListDirectory($remotePath)
# $directoryInfo = $session.ListDirectory($localPathPath)
# Select the most recent file
$latest =
Get-ChildItem -Path $localPath |
Where-Object {!$_.PsIsContainer} |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
# Any file at all?
if ($latest -eq $Null)
{
Write-Host "No file found"
exit 1
}
# $latest.Name
$sourcePath = Join-Path $localPath $latest.Name
$session.PutFiles(
[WinSCP.RemotePath]::EscapeFileMask($sourcePath),
[WinSCP.RemotePath]::Combine($remotePath, "*")).Check()
}
finally
{
# $session.Dispose()
}
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
I'm not sure why it's not uploading the latest file from my local drive to a remote host? Any ideas would really appreciate it.