WinSCP batch transfer with SQL Server Agent Job - batch-file

I have a .bat file that:
executes WinSCP (calls a script txt) and transfers a file through SFTP. It also archives the file that was transferred.
My Problem is it works just fine when I double click the .bat file. BUT when I execute it in SQL Server agent job, it does archive the file but is not transffering the to the SFTP.
I get no error at all in the history of the job.
I feel it isnt executing the WinSCP or not opening the txt script. I checked permissions and it shows fine in all files and folders...
Checked FULL control for all users on all files and related folder.
Tried checking my syntax.
"C:\Program Files\WinSCP\WinSCP.exe" /command "option confirm off"
/console /script="C:\Program Files\WinSCP\LocalToRemote.txt"

You should add /log=C:\path\to\winscp.log switch to WinSCP command line to see what's going on.
In a rare case the error is so fatal that even no log file is produced, you should capture WinSCP console output. You cannot do that with winscp.exe, you need winscp.com for that, and you should use it anyway.
"C:\Program Files\WinSCP\WinSCP.com" ^
/log=C:\path\to\winscp.log ^
/command "option confirm off" ^
/script="C:\Program Files\WinSCP\LocalToRemote.txt" > C:\path\to\winscp.out
Until you show us the logs, I can only point you to WinSCP FAQ:
My script works fine when executed manually, but fails or hangs when run by Windows Scheduler, SSIS or other automation service. What am I doing wrong?
The most usual problem is the first one listed in the FAQ:
The script fails (or “hangs”), because the host key (SFTP or SCP) or certificate (FTPS, WebDAVS) is not known by the service’s account, and WinSCP fails (or asks for) its verification.
See also Debugging transfer task running in Windows Scheduler, SSIS, or another automation service.
The best what you can do it to have WinSCP GUI generate script/command-line/batch-file template for you, with all details needed for the script to be really portable.

Related

CMD, connect to SFTP with cmd command and copy file from there [duplicate]

I have program in Unix that generates files 8 files at 8:30 pm everyday.
I need to download the files into my Windows machine after the files are generated using WinSCP.
I can drag and drop those but its time consuming, I need to automate this process.
I cannot use .Net assembly to download those.
I have tried to use the get command, but its throwing error: Not an SCCS file.
Let me know how can I solve this.
--Thanks
To automate a task using WinSCP, use its scripting interface from a batch file (e.g. download.bat).
I assume you want to use SFTP, as you are connecting to a *nix server.
The simplest download batch file is like:
winscp.com /log=c:\path\to\log\winscp.log /command ^
"open sftp://username:password#example.com/ -hostkey=""xxx""" ^
"get /path/to/file.ext c:\path\to\download\to\" ^
"exit"
Replace the username, password and example.com with the actual connection details. Get the value of -hostkey switch from your GUI session. Use real paths.
Though it's easier to have WinSCP generate the batch file for you.
For details see a guide to automating file transfers from SFTP server using WinSCP.
Once you have the batch file working, schedule the the batch file execution using Windows scheduler.
See a guide to scheduling file transfers from SFTP server.

Batch or .exe via SSIS package fails to give output despite of success status of SQL Agent Job

I have created a batch file which starts a command line (IrfanView) with several arguments. The batch looks like this:
start /wait /d "C:\Program Files (x86)\IrfanView" i_view32.exe C:\Source\*.png /advancedbatch /convert=C:\Destination\*.jpg
Basically it converts all PNG from source folder to JPEG using advanced batch settings stored in INI of IrfanView to destination folder.
The batch file is located on my server (same as IrfanView) and when manually started it works fine. I also tried to start the batch using SSIS execute process task, which also works when package executes manually.
Another option was to start IrfanView directly from the execute process task (so no batch file) and then manually trigger the package. Every option seems to work manually.
However, whenever I try to run the batch (cmdExec) or any package in SQL Agent it will run the job successfully, but... there is no output in the folder. So, it doesn't give an error, it simple does nothing.
When run manually the output files will appear as expected in the destination folder.
When run through SQL Agent no output files will appear although the job was run successfully.
Could it have to do with security settings? SQL Agent runs under local user with full administrator rights.
I have read something about credentials and proxy, but not sure how to use it and if it will solve the problem.
I have another job running which also calls an executable (7zip) in a SSIS package and it works fine.
If I use another program, for example Flash Renamer from command line, then the SQL Job keeps running (status in progress), while triggering the package or batch manually it works fine.
Using SQL Server 2008 (BIDS + SSMS)
How can this be resolved?
Command start often interprets first double quoted string as title string. Therefore it is necessary to specify a title in double quotes if any other string must be enclosed also in double quotes.
IrfanView does not require that its program directory is the current working directory. Therefore simply starting IrfanView with full path would be better in my point of view.
IrfanView option advancedbatch requires data from i_view32.ini. On running IrfanView with a different account, it will be necessary most likely to specify the path to the folder containing i_view32.ini.
i_view32.ini is usually stored either in program files directory of IrfanView or in directory "%APPDATA%\IrfanView".
The first option is usually not used anymore as default program files location "%ProgramFiles(x86)%\IrfanView" on Windows x64 or "%ProgramFiles%\IrfanView" on Windows x86 is write-protected by default for users of Windows Vista and later Windows versions.
Therefore i_view32.ini is nowadays by default in application data directory of IrfanView of current user account.
Yes, environment variable APPDATA contains name of the user account and differs therefore from user account to user account. And by default user X has no permission to access the application data directory of user Y.
So on running IrfanView with a different account, file i_view32.ini or a copy of it containing the advanced batch conversion options must be located in a folder readable for all user accounts or at least the used account and on command line the path to this folder must be specified, too.
With summarizing up all information above, you would need most likely
start "Convert PNG to JPEG" /wait "%ProgramFiles(x86)%\IrfanView\i_view32.exe" C:\Source\*.png /advancedbatch /convert=C:\Destination\*.jpg /ini="Path to folder with i_view32.ini" /cmdexit
But below should also work if the batch file does not contain any other commands and therefore no batch file is needed at all.
"%ProgramFiles(x86)%\IrfanView\i_view32.exe" C:\Source\*.png /advancedbatch /convert=C:\Destination\*.jpg /ini="Path to folder with i_view32.ini" /cmdexit

Batch file's Core FTP line is Not running during Scheduled Task. Works if started Manually

I have a simple batch file which needs to be run weekly to upload some files via Core FTP.
I'm using the free version of Core FTP LE.
MySavedProfile is the Site Name of the saved profile I created using Core FTP's site Manager. The profile contains the URL / credentials / etc of the site to connect to.
Here are the contents of the batch file:
SET logf=confirm.log
echo test-start >> %logf%
"C:\Progra~1\CoreFTP\coreftp.exe" -B -s -pasv -O -site MySavedProfile -u "C:\Progra~2\PathToFiles\FileToUpload.txt"
echo test-finish >> %logf%
For the Windows Server 2012 r2 Task Scheduler, I have created a basic, weekly scheduled task on the Task Scheduler Library root which runs the batch file. For this scheduled task I have:
(Under the General tab)
"Run whether user is logged on or not" is selected
"Run with highest privileges" is checked
Configure for = Windows Server 2012 R2
(Under Actions)
Action = Start a program
Program / Script = "C:\Progra~2\PathToFiles\batch.bat"
Start in = C:\Progra~2\PathToFiles\
Here is the weird behavior I am getting:
If I double click on the batch file directly, it works fine and uploads the text file via Core FTP just fine.
However, if I try to let the Windows Task Scheduler run it, it runs everything except the Core FTP line. That is, I get the usual:
test-start
test-finish
in the confirm.log file, but the FileToUpload.txt has not been uploaded to the remote server, and there are no errors from CoreFTP that I can detect.
I have tried this with a service account that has permissions to run batch files, as well as my own account for this scheduled task. I get the same result: it doesn't seem to run that CoreFTP line. At least not via Task Scheduler. I need this upload to be automated.
I've searched Core FTP's documentation, Google, etc. No one seems to have run into this exact issue. I've applied recommendations from distantly related issues, but none of them have worked.
Any help would be greatly appreciated. Thank you.
The only way to do this is to use the full version of Core FTP (that is Core FTP Pro). If you use the LE version you have to check the "Run only when user is logged on" option.
This happens because of the splash screen at the beginning.
If you can't be logged on forever you could create a user that will always be logged on just for these tasks.
Remember to use the -Log option on CoreFTP to check if it is actually doing something.

Text File Permissions Issue

What is the default SSIS Account that is used in SSIS packages.
In one of my packages I have a Data Flow task that creates a flat file. A BAT file later runs and creates a file based on information in that .txt file. If I execute the .BAT in Windows Explorer it runs fine. When SSIS tries to execute it I can see the CMD window open and it tries to access the txt file and isnt able to. Says "Unable to access nameoffile.txt"
I assume the issue is that the permissions are probably inherited by the SSIS user account so I am trying to figure out which account that is.
If you are running it from the IDE then it uses your permissions. Most likely the problem is that your file is locked by some other process in your SSIS package. To verify:
Add a PAUSE to your batch file to make it wait for any key.
Run your package. While the dos prompt is waiting, go into windows explorer and run your batch file again - you'll find the same error.
I changed the attrib value in dos for that specific folder by attrib -r -s and it worked. It thought it was read-only.
I am using Windows Server 2012 server, with a SQL Server 2012 database.
Adding Modify, Read, Write permissions to the folder containing the required file to the NETWORK SERVICE user seems to work for me.

Batch runs manually but not in scheduled task

I don't generally write batches, but I currently have a batch that uses forfiles to copy my FLVs from one folder to another. When I run the batch manually it works every time, but from a scheduled task, it throws a (0x1) error.
forfiles -p "C:\Program Files\Adobe\Flash Media Server 4.5\applications\name\streams" -m *.flv -s -d -1 -c "cmd /c copy #file ^0x22C:\Program^ Files\Adobe\Flash^ Media^ Server^ 4.5\applications\name\output\"
Not sure what syntax the scheduled task doesn't like.
Update
Under my scheduled task, Actions I have the following:
Program/script: name.bat
Start in (optional): \\servername\file\to\batch
Hi might be this is helpful,
I also face the same issue.
Just set the startin path like:
Here start in path is the path of batch file:
like you have enter in program script:
"E:\program related files\demo.bat"
then in startin just pass:
E:\program related files & done!
When my Start in (optional): path was a UNC path, it wouldn't work. So I moved my batch on the server and everything worked correctly.
UNC in Windows Batch Files
forfiles with UNC path
On Server 2008 R2 when running the batch file under domain user credentials, with confirmed "log on as a batch job" security in the Local Security Policy>Local Settings>User Rights Assignment,
even then my batch (copying a log file to a network share) would not run as scheduled task, until I selected in tab General the option "Run with highest privileges" (default NOT checked!)
The option Run whether user is logged on or not was also selected, with radio-button, but I guess this is quite standard, when selecting to run the task using a domain user account.
For the tab Actions : specifying the entire batch file name including its path, directly in "Program/script:" works fine (with Server 2008 R2)
Using double quotes inside the batch file causes no problems.
See the screen shot bellow.
You need to change the user to system
Most common reason for such problems is permissions: scheduled tasks does NOT always run with your user credentials. If you want scheduled task to run as you you will have to set it up as you or alternative user.
Besides that I hope that your line of code is a content of your batch file, you are not trying to run this command directly. Or are you?
P.S. What are these ^0x22 and ^ doing in your code?
I know this is an old question, but just wanted to share some info.
The (0x01) error code can also refer to resources that are not found. Therefore:
all files/folders referd from within the batch file should be accessible to the user which account is being used to run that scheduled task;
pay attention when using network locations in combination with "Run whether user is logged on or not" option;
the above-mentioned option can be tricky to use because some resources may be available only after log on.
For .bat files to run inside your scheduled task, you need to specify your .bat file path inside the start option - despite the fact that your .bat file is at the same directory as your .exe. Also, I flagged it to run with highest privilege. After I have done those two things, the task suddenly takes off without any problem!

Resources