Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)


Topic review


Re: Won't Upload latest file from local to remote host

Hi Martin,
Thanks for your reply. I have tried what you mentioned below:
powershell.exe -File C:\users\folder\scripts\LatestFile.ps1 using the Task Scheduler. The operations ran successfully, but still no file got copy to the remote host. I wonder if my local path to the file is on a mapped drive like H:\sourcefile\uploadfromhere\ has anything to do with it? Meaning, it should be able to see my mapped drive, which is H drive, because I can mapped to it.
Also, I inserted a line for the log:
$session = New-Object WinSCP.Session
$session.SessionLogPath = "C:\Users\thucn\scripts\outputUploadLog.log" , but no log file has been generated. Does that look right?
Again, have I missed anything?

Re: RE: Won't Upload latest file from local to remote host

You cannot execute PowerShell script via WinSCP.
If your script works when you execute it manually, you for sure do not execute it like:
"c:\program files (x86)\Winscp\winscp.exe" /log="c:\users\folder\scripts\winscp.log" /ini=nul /script="C:\users\folder\scripts\LatestFile.ps1"
You execute it like:
powershell.exe -File C:\users\folder\scripts\LatestFile.ps1
So do the same in Task Scheduler.

RE: Won't Upload latest file from local to remote host

Do it manually it works.
Here's my task scheduler setting:
Start a program: "c:\program files (x86)\Winscp\winscp.exe"
Add arguments (optional): /log="c:\users\folder\scripts\winscp.log" /ini=nul /script="C:\users\folder\scripts\LatestFile.ps1"
Start in (optional): C:\users\folder\scripts\
The return result (0X1) and no latest file ever been copied to the remote host.

Re: Won't Upload latest file from local to remote host

Does the script work when you execute it manually? What does it output?

Won't Upload latest file from local to remote host

Newbie to WinSCP and would like your help. I follow this sample script here: 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:

   # 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
      # Connect
      $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
            [WinSCP.RemotePath]::Combine($remotePath, "*")).Check()
      # $session.Dispose()
    exit 0
    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.