Solved

Windows Task Scheduler running WinSCP Batch File

Posted on 2014-01-07
8
5,977 Views
Last Modified: 2014-01-08
Hi everyone,

I'm currently in a bit of a pickle regarding some scripts I have been writing.
I am creating a script that uploads multiple files from an FTP server that my company owns, over to another FTP server that an associate company is in control of. I have successfully transferred files to and from each of the servers via an automated method of using the Windows built-in FTP client and Task Scheduler, but the problem this time is that the nature and sensitivity of the files being transferred means we need to move them over using SFTP, something that Task Scheduler isn't too keen on running properly.

Now I am aware that there is no native SFTP client for Windows, so after a bit of research I decided to install WinSCP and have a crack at automating a script that would do the process that is required.

Here is a little information to explain what the script does...

Server (A) is my FTP server. There is a folder on this server that has 6 files within it, all of which are overwritten once an hour, each day of the week. Lets call this Folder (A).
Server (B) is our associate FTP server. There is a folder on here that the files are to be transferred to. Lets call this Folder (B).
Once the transfer between Server (A) and Server (B) has been completed, the original files held on Server (A) need to be moved from their current location of folder (A) to an archive folder named Folder (A2) on the same server. This is to preserve the integrity of the files.

The script is written using WinSCP to perform the initial transfer. This is then fired using a Batch file that runs the script using certain parameters/commands, and then proceeds to perform an XCOPY command to move the files to an archive folder on the same server.

Now the batch file runs perfectly when firing it manually (double-clicking it), but when setting it up as a scheduled task in Windows Task Scheduler, it fails to run correctly. The task will run the XCOPY command perfectly as I can see the files moving on Server [A] from folder to folder, however it is not performing the WinSCP portion of the script. It's just skipping it out.

Here's the WinSCP script "testscript.txt"...
option batch abort
option confirm off
open sftp://username:password@serverbaddress -hostkey="ssh-rsa 2048 XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX"
cd /FolderB
option transfer binary
mput "C:\FTPRoot\FolderA\*"
close
exit

Open in new window


And the batch file that is running the SFTP commands...
"C:\Program Files (x86)\WinSCP\WinSCP.com" /script="testscript.txt" /ini="C:\Program Files (x86)\WinSCP\WinSCP.ini" /log="logscript.txt"
xcopy /y "C:\FTPRoot\FolderA\*.xml" "C:\FTPRoot\FolderA2\"

Open in new window


Can anyone point out what may be wrong here?
I have already tried the solution in THIS ARTICLE, however It hasn't seemed to work for me. Even forcing the scheduled task to run by right-clicking it and choosing 'run' gives me the same problem, so there is obviously something that Task Scheduler doesn't like about the batch file.

Any help is greatly appreciated! I'm getting dizzy going round in circles this much...
0
Comment
Question by:Beny9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 13

Expert Comment

by:Jeff Darling
ID: 39761898
What version of windows are you running this on? I know there are some key differences between them regarding the task scheduler.
0
 

Author Comment

by:Beny9
ID: 39761935
@jeffld - We're running Windows Server 2008 R2 Standard, with Service Pack 1 installed.
0
 
LVL 13

Expert Comment

by:Jeff Darling
ID: 39762086
Can you try creating a test batch file to see if the executable is firing?  

I do not have a windows server 2008 to test this on.

I did a simple test to display the help.  I also passed a test argument to my batch program and redirected the output to a text file so I could see the results.  Can you try that on your end?

@echo off
echo ----------------- >test1.txt
echo test parm: %1 >>test1.txt
echo ----------------- >>test1.txt
"c:\program files\winscp\winscp.com" /help >>test1.txt

Open in new window


then run the test schedule and see if it created the test1.txt file and what it contains.

here is what mine contains when I did the test.  I'm testing with the latest version of winscp.

-----------------
test parm: testarg
-----------------
WinSCP, Version 5.5.0 (Build 3839)
Copyright (c) 2000-2013 Martin Prikryl

Usage:
WinSCP [/script=file] [/command command1...] [/parameter param1...]
WinSCP /ini=<inifile> /log=<logfile> /xmllog=<logfile>
WinSCP /help

 /script=      Executes batch script file. If the script does not end with
               'exit' command, normal interactive mode follows.
 /command      Executes list of script commands.
 /parameter    Passes list of parameters to script.
 /ini=         Path to configuration INI-file.
 /log=         Turns on session logging to file.
 /xmllog=      Turns on XML logging to file.
 /help         Prints this usage.

Open in new window

0
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 

Author Comment

by:Beny9
ID: 39762146
@jeffld - I created the test batch file the same as yours, although changed the executable path from "c:\program files\winscp\winscp.com" to "c:\program files (x86)\winscp\winscp.com" as WinSCP runs in a 32bit environment on Server 2008 R2 so is placed in the "Program Files (X86)" directory instead.

It generated the same help text output file as yours...

----------------- 
test parm:  
----------------- 
WinSCP, Version 5.5.0 (Build 3839)
Copyright (c) 2000-2013 Martin Prikryl

Usage:
WinSCP [/script=file] [/command command1...] [/parameter param1...]
WinSCP /ini=<inifile> /log=<logfile> /xmllog=<logfile>
WinSCP /help

 /script=      Executes batch script file. If the script does not end with
               'exit' command, normal interactive mode follows.
 /command      Executes list of script commands.
 /parameter    Passes list of parameters to script.
 /ini=         Path to configuration INI-file.
 /log=         Turns on session logging to file.
 /xmllog=      Turns on XML logging to file.
 /help         Prints this usage.

Open in new window

So I gather we're running the same version of WinSCP.
0
 
LVL 13

Accepted Solution

by:
Jeff Darling earned 500 total points
ID: 39762506
I suspect that winscp cannot find the script file.  can you explicitly code the path?

"C:\Program Files (x86)\WinSCP\WinSCP.com" /script="c:\pathtofile\testscript.txt" /ini="C:\Program Files (x86)\WinSCP\WinSCP.ini" /log="c:\pathtofile\logscript.txt"

Open in new window


where pathtofile is the actual path.

also, might try redirecting the output of the above line to a text file to see what it produces.
0
 
LVL 16

Expert Comment

by:AlexPace
ID: 39764045
When something like this runs when you run it manually but fails as a scheduled task the first thing to check is which windows account is used to launch the task.  If it is a network folder you need to use a login that has access to the folder.  If it is a mapped drive you might have to use a UNC path or run net use within the task since the mapped drive is associated per login session.

I can't offer any specific advice with WinSCP script but the following Robo-FTP script will do the job described above:
SET local_source = "C:\FTPRoot\FolderA\"
SET local_archive = "C:\data\archive\A2\"
SET remote_address = "serverb.address.com" ;; or use IP address
SET remote_folder = "/FolderB"
SET remote_username = "UserID"
SET remote_password = "Secr3t!"
SET remote_server_type = "SFTP" 

:loop_start
WORKINGDIR local_source
ARCHIVEDIR local_archive
FTPLOGON remote_address /user=remote_username /pw=remote_password /servertype=remote_server_type /trust=all
FTPCD remote_folder
SENDFILE "*" /archive 
FTPLOGOFF 
:loop_done
CRON "@hourly" ;; sleep until the top of the next hour 
GOTO loop_start

Open in new window

It is a loop that runs once every hour.  The idea is that you'd install it as a Windows service so that it is just running all the time and wakes up once per hour.  If you really want to use a scheduled task instead just remove the bottom two lines. This page shows how to run a Robo-FTP script as a Windows service: http://www.robo-ftp.com/help/3.9/service_installer.htm

EDIT: You would need to modify the first 6 rows to match your environment
0
 

Author Comment

by:Beny9
ID: 39764543
@jeffld - I added the full file path to the script and logscript sections of the code and it seemed to work yesterday evening before leaving work. I then decided to leave it overnight and check the logfile and the remote server to see if the files had been sent over successfully.

Hurrah! The scheduled task has run as it should have, and has run each line of the script exactly as needed. I should've known to check the file path, but it was something that I assumed wasn't necessary as the script is located in the same folder as the batch file. How strange that it would generate the logscript.txt file perfectly fine, yet wouldn't run the script itself. Many thanks for opening my eyes up to such a simple issue!

@AlexPace - I had been looking at RoboFTP the past couple of days and was tempted to start working on that instead. If I hadn't found a solution to this by the end of the week, I would have switched over to this method.
0
 

Author Closing Comment

by:Beny9
ID: 39764545
Spot on! Can't believe I hadn't tried that yet. Much appreciated!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question