Cannot run a batch file from SQL Server Agent (Access denied) - sql-server

I am trying to execute a batch file from SQL Server Agent (as it needs to be done before some SSIS-packages are run).
When I execute the job it fails in a few seconds saying "Access denied".
The account under which SQL Server Agent runs has full control on the folder that contains the batch file. The result of the batch would be deleting some files in a folder, calling a webservice and get those same files back from the webservice.
I can run the batch file when I start it with my own (admin) account.
I googled and found several other questions and answers but none of those were covering my problem. I hope you can point to other options.
Thanks for your help.
Johan
Batch file contents:
echo Removing txt files of last run
del Employees.txt
del HrDepFun.txt
del HrEmpActual.txt
echo Files removed
echo Starting getconnectors
{Call Webservice} -> cannot disclose this on stackoverflow
echo Getconnectors done
Batch file execution statement from SQL Server Agent job (type Operating System (CmdExec)):
cmd.exe /c "c:\Program Files (x86)\AFAS\AFASRemote_Call_GetConnectors.bat" > connectorlog.txt 2> connectorerrorlog.txt

Related

WinSCP batch transfer with SQL Server Agent Job

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.

Automatically run .bat file after EC2 reboot without having to remote connect

I am running an AWS Windows 2012 EC2 instance that has to run 24/7. On this instance, I run a Python 3.6 scraper script and to prevent me from having to regularly check up on the server whether the file is running, I have a .bat file in the shell:startup folder of my instance, that automatically restarts it on a daily base. The .bat file works as it will run the Python script and set a timer to restart/reboot the instance after (t=86400). The .bat file runs on the EC2 instance itself.
However, what the file does not do is run automatically after the reboot. I now first have to remote connect to the server before the .bat file will run. What I want it to do is run without me having to first remote connect into the server. How can I achieve this?
I use the following code in my .bat file. Located on my EC2 instance.
#ECHO OFF
START CMD /K (
CD C:/Users/Administrator/Documents/
python scraper.py
)
START CMD /K SHUTDOWN -t 86400 -r -f
I have tried looking into using AWS' Automations and other schedule based methods but couldn't get that to work.
If you want to use something native to Windows Server 2012, look at Schtasks -- this is more or less the Windows equivalent of cron.
I found the answer to my question by using Task Scheduler and looking at the following article: Run a batch file with Windows task scheduler
An important note here is that for my batch file to run I had to create a task that started CMD and run the batch file from there. Asking Task Scheduler to run the batch file directly doesn't work on Windows Server 2012. I ran the task with the following details:
Administrator account
"Run whether user is logged on or not"
"Run with the highest privileges"
"Start on system start-up"
Action: Start a program -> CMD
Add arguments (optional): /c start "" "C:\Users\Administrator\Desktop\file.bat"
More information on how to do this can be found in this answer: https://stackoverflow.com/a/27055435/7736676

Need help writing .bat file which changes all file permissions in a folder to Everyone

I appreciate your time so I won't take much of it. Here's what I'm trying to do: I want to create a .bat file that, when executed, will open the Command Prompt program, change directory to a folder (located on the C:), then execute a line of code that will change the file permission status of all files in that folder to Everyone. I intend to create a Windows task that will run this .bat file everyday. I understand how to setup the Windows task, but I can't figure out exactly how the .bat file should be written. Can anyone help?
Here's what I've got so far:
ECHO OFF
[Tab]Start "" C:\Windows\system32\cmd.exe
ECHO OFF
[Tab]Prompt cd cd:\google drive
ECHO OFF
[Tab]Prompt cacls *.* /t /e /g everyone:f
If you're interested, here's why I'm trying to create this .bat file:
I use a single Google Drive account on two file server computers, one in each of two offices. Each office has this file server and about 10 client computers. The client computers access files on the shared Google Drive folder (located on the file server) and occasionally add/edit/delete files. Google Drive does a great job of keeping all files synced between the two offices, but one problem I have is that if office A adds a file, the only computer in office B that can see it is the file server. I have to change the file permission to "Everyone" using the file server in office B before any of the client computers in office B can see the file. Over time, it's become very annoying to manually change the file permission every day and I'm looking for a shortcut. Please let me know if you can think of a better one.
If you got the commands right then this is a batch script with them.
The && will run the following command only if the cd command was successful.
#echo off
cd /d "c:\google drive" && cacls *.* /t /e /g everyone:f

Am trying to run a batch file from user machine on domain to update program on server

this is what I need to achieve is to update data on a server from a users machine. I have used net use and that part works, but trying to run the program doesn't. Here is my batch file:
#echo off
net use \\servername /user:admin admin <- This works and logs in "command completed successfully
"C:\program files\demo\dm.exe" /r "D:\demo\demoDeploy\roll\dem_dem_Extract.dmv" <-this however gives error message:
The system cannot find the path specified.
any help will be appreciated

Executing an sql batch file within a seperate batch file

I am running a 2008 SQL server. I am using remote backup to backup the server and have two batch files to trigger the backup process. The first batch file contains:
#echo off
CALL "" "C:\Scripts\SQL Maintainence\Nightly Maintenance 2008 SSE.bat"
The second batch file (Nightly Maintenance 2008 SEE) looks like this:
#echo off
osql -S %SERVER% -d msdb /U (username) /P (password) -i "Nightly Maintenance 2008 SSE.sql" -o "Nightly Maintenance 2008 SSE.txt"
For some reason, the first batch file is not calling for the second batch file to run, the script works when manually ran.
I am very new to writing batch files and have done quite a bit of research up to this point. Any help or maybe an article that can help with my issue would be greatly appreciated.
Reposted as answer instead of question...
I think you need to delete the "" immediately after the CALL, like this:
CALL "C:\Scripts\SQL Maintainence\Nightly Maintenance 2008 SSE.bat"

Resources