Won't Upload latest file from local to remote host

Advertisement

cp713a
Joined:
Posts:
7
Location:
usa

Won't Upload latest file from local to remote host

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.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,276
Location:
Prague, Czechia

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

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

Reply with quote

cp713a
Joined:
Posts:
7
Location:
usa

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.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,276
Location:
Prague, Czechia

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"
Right?
You execute it like:
powershell.exe -File C:\users\folder\scripts\LatestFile.ps1
So do the same in Task Scheduler.

Reply with quote

cp713a
Joined:
Posts:
7
Location:
usa

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?

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum