Sql-server – Command runs fine in elevated command prompt, but fails when run in a Windows Task Scheduler task

scheduled-taskssql serversql-server-expresst-sql

Here's my predicament:

  1. The version on the server is SQL Server Express. And no, we cannot use any other version. (Long story, but: vendor will not allow without $ome…)
  2. From 1, you can deduct that there is no SQL Server Agent jobs.
    Hence the need to automate SQL Server backups "creatively".
  3. We did so years ago, and it was working perfectly for years (more than 2).
  4. What did we do? We created the command string sqlcmd -U SQLBackupOper -P <password> -S GSUDM -Q "EXEC sp_BackupDatabases".
  5. Yes, sp_BackupDatabases is a stored procedure in the Master database that backs up all DBs.
  6. Yes, if I login to the server and open an elevate (as administrator) command prompt and run the command in number 4, it works perfectly. So, the password is correct. I copied and pasted several times to make sure, and no, the problem is not the password.
  7. Yes, the login SQLBackupOper is mapped to all DBs and has the role membership of db_backupoperator.
  8. We also created a task in the Windows Task Scheduler that is meant to run the command string in number 4, and it worked for years. Now, for some indeterminate time (I am not sure how long) the task is failing. When I look at the SQL Server error logs I am greeted with the error that reads Login failed for user 'SQLBackupOper'. Reason: Password did not match that for the login provided.
  9. Yes, the password in the command string is correct. I tested it from the command prompt via copy and paste. Again, it works from the command prompt. It only fails if run from the task scheduler, the same exact command string.
  10. Yes, I did try varying the user that runs the job. Still failing.
  11. Yes, I do run the task with the highest privileges.

With that said, has anyone faced this problem before? Again: It works from an elevated command prompt, but it complains when using the same command string citing wrong password when run as a task in task scheduler. Thoughts? Suggestions?

Best Answer

Thanks guys I resolved the problem. Although I'm not sure what I did that resolved it. What I did:

Created a brand new SQL Server login with sysadmin rights (I know it shouldn't need that much, but hey, I needed to fix this.) Added a new local user login in users and computers MMC in the SQL Server hosting the machine, and made it a local admin to the machine. Updated everything to use those users, instead of what we had before, and suddenly it started working. So, it's something related to permissions, most likely for the SQL Server login. But still, as far as I can tell the initial user had everything it needed. So much so that I could confirm that to be the case both using a command line as well as a batch file.

Thanks for trying to help. Closing this here. Best, Raphael.