Scheduled sqlcmd Task not working - batch-file

The following are performed via Task Scheduler, the two commands are run using the same task (just altering the trigger time) and I logoff the server before the task trigger time occurs.
If I create a batch file for the following command it executes fine (it creates the log files and writes the sqlcmd output to the file), furthermore the output in the log file is 1, which explains that the account running the batch file has permission to execute the stored procedure:
sqlcmd -E -S .\SQLEXPRESS -d WEDFUTtest -Q"select HAS_PERMS_BY_NAME('sp_DBMaintenance', 'OBJECT', 'EXECUTE')" -o %FILENAMEANDPATH%
If I edit the batch file (so it maintains the same permissions etc) with the following command:
sqlcmd -S .\SQLEXPRESS -E -Q "EXEC sp_DBMaintenance #dbName=TestDB, #FragmentationThresholdForReorganizeTableLowerLimit='5.0', #FragmentationThresholdForRebuildTableLowerLimit='30.0'" -d TestDB -o %FILENAMEANDPATH%
And logoff and allow the Task Scheduler to run it, it does not work i.e. there is no log file and the whole task executes in a very short time, sometimes under 1 second... and I know that the sp_DBMaintenance script takes over 20 minutes to finish... there are no error messages...
Furthermore (logged in on the domain administrator account) I can manually run the 2nd (non-working) command manually either via cmd or by right clicking the task in Task Scheduler and everything works fine.
So the question is, why isn't the 2nd command working correctly?
P.S. %FILENAMEANDPATH% is of the form SP_Log_23-11-2016_11-41-00.log

I'll put the 'answer' here in case it helps anyone encountering a similar issue...
Both the 1st and 2nd command have identical settings for the Task Scheduler...
On the off chance I unchecked the security option "Run with highest privileges" and the 2nd command now works?!?!?!?!? I don't understand why but now it works...
Taken from Technet
"If you select the checkbox labeled Run with highest privileges, Task Scheduler will run the task using an elevated privileges token rather than a least privileges (UAC) token..."
What baffles me is how the 1st command executed sqlcmd with 'Run with highest privileges' but the 2nd sqlcmd only works without it?!??!?!?!?!

Related

Why does the batch file I created to stop and start a windows service not working correctly

My batch file that I created to stop and then start windows service won't run and throws back an error
Each time I run the file as Admin (I am the only user that's on this machine and the account is an admin account).
I have tested my batch file on its own and not in task scheduler and it works perfectly fine when I run the batch file as admin. However, it falls over when I try set up a daily task on Task Scheduler.
I have a simple batch file that stops and starts a service. for reference this is what it looks like:
Net Stop "StorSvc"
Net Start "StorSvc"
I have run this as admin and it worked fine. I then created a task to do this daily at a certain time. I placed the file in C: Drive and attached the file to my task scheduler.
On the security options I have ticked
"Run whether user is logged on or not"
"Run with the highest privileges"
I have changed the "When running the task use the following user account" to my account, Systems and other admin options that show up. I even selected "System"
When I click ok it prompts me to sign in to the admin account. when I do this it says
"An error has occurred for task StorSvc" Error message. One or more of the specified arguments are not valid"
All of the last run results are as listed below:
0x800710E0
0x41303
0x2
I have had better success with sc.exe for controlling services via batch file.
For your situation this should work:
sc.exe stop StorSvc
sc.exe start StorSvc
If that still gets credential issues you could include PsExec.exe like this:
psexec.exe -u <user> -p <password> -c "sc.exe stop StorSvc"
psexec.exe -u <user> -p <password> -c "sc.exe start StorSvc"

Scheduled Task Runs .bat File with sqlcmd, but Doesn't Update the Output File

My scheduled task (on Windows Server 2008 R2) is "successfully" running each morning, but the output of sqlcmd does not get updated in the output file. The log file changes to show that the batch file ran, but the output file remains the same.
When I run a .bat file with the lines of code below, it works just fine. The .csv file gets updated with the most recent query's results.
sqlcmd -S SERVERNAME -i my_query.sql -s "," -o c:\scripts\My_query\query_results.csv -W -h-1
ftp.exe -s:"c:\scripts\My_query\file_upload.ftp"
Useful Information:
I am running the scheduled task as my user which is a server admin and has access to the server in question
I have tried giving explicit access to the user for full control of all files and folders involved
I have tried running C:\Windows\System32\cmd.exe as the action with the argument /c C:\scripts\My_query\my_process.bat to make sure the entire process runs
Task scheduler is working fine for other tasks, but it has this same problem with five similar processes
Are the tasks configured to run whether the user is logged on or not? If so, it will never find your SQL file because the default working directory is SYSTEM32 when you configure a scheduled task that way. You will need to provide the full path to the SQL file.

Using Task Scheduler to run a batch file that contains the start /w command

I have a Windows XP virtual machine that I am using to run a legacy app. I am using a limited user account.
I have created a batch file that I'd like to:
Start the legacy app
Wait for the program in step 1 to close
Shut down the computer
So this is the code I am using (in this example, I am attempting to open notepad):
#ECHO OFF
start /w notepad
%windir%\system32\shutdown.exe -f -s -t 1
Because the legacy app needs to be run under an account with administrator privileges, I then manually created a scheduled task in Task Scheduler to run the batch file. The scheduled task is set to run using an account named Admin that has administrator privileges.
I am having a bit of an issue. When the scheduled task runs, nothing appears on the screen. However, I did notice that both cmd.exe and notepad.exe appear in Task Manager as processes running as the user Admin.
What am I doing wrong? How can I accomplish what I'd like to do? Thanks!
Sorry all, it was an issue with me not having Task Scheduler set up properly. I found this post which suggests to use the AT command to run the task interactively. This allows the Cmd window to be displayed when a batch file is run as a Scheduled Task.

Automate backup command not working in job scheduling

I am using PostgreSQL 8.4 on my Windows Server 2012 box. I have made a configuration with pgagent for job scheduling. I need to schedule an auto backup on a daily basis on my system using with job scheduling. I searched the net and created a batch file and located the file path in job scheduling.
If I run the batch file on my server then it works fine, but when I call it using scheduling in PostgreSQL then it goes in the running stage, but does not give any kind of result.
Below is the command I used for taking backup:
"c:\Program Files (86)\path\to\bin" pg_dump.exe -i -h hostname -U username -F c -b -v -f "backup\file\path\filename.backup" databasename
This command is working fine on the command prompt and when calling the batch file, but does not give any output from PostgreSQL job scheduling.
Does anyone have any idea about this kind of issue?

Executing a stored procedure using Windows task Scheduler

I've been trying to set up a schedule to run a stored procedure every hour in Windows Task Scheduler (as I'm using SQL Express and can't install 3rd party tools) but after trying various methods such as running a .bat file from task scheduler, opening SqlCmd utility from task scheduler and passing either the command line syntax or a .sql script file I'm having no luck.
I know this can be done and therefore I'm sure it's something I've missed but if anyone can share their experience of this I'd very much appreciate it.
The following command is in the batch file...
sqlcmd -E -i"C:\Users\Administrator\Desktop\test.sql" -o"C:\Users\Administrator\Desktop\dump.txt"
Thanks a lot
If you are an admin on the sql instance (Since you are using SQLExpress I bet you are trying to do this on your own computer so there is a high chance your user is an admin of the sql instance) you should not use -E at all, just ignore it.
Second, specify the server even if you are working on local.
Start with a simple sql command like below:
sqlcmd.exe -S "." -d MY_DATABASE -Q "SELECT * FROM MY_TABLE"
Replace MY_DATABASE and MY_TABLE with your dbname and table name. Make sure you can run it from command line. It should return the data from your table. (Beware command line options are case-sensitive so -s is not same as -S)
Last, do not try to feed parameters through task scheduler. Put the command with all parameters in a .bat file and just run the batch from task scheduler.
I have recently had a similar issue and my experience may assist you. I was calling a small app i.e. EXE from a batch file. I was scheduling the batch file to run from the Windows Task Scheduler. The app was accessing the SQL data using Windows Authentication.
I could run the app directly i.e. click on the EXE to run it.
I could run the app from the batch file.
But if I tried to run the scheduled task it seemed to start but did nothing and posted no errors that I could find.
I found if I changed the app to run with SQL Authentication it could be run from the Task Scheduler.
I suspect there is something about the context of the Windows Authentication when it is run from Task Scheduler that is not recognised by SQL.

Resources