title: Scheduled sqlcmd Task not working
body: <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>sqlcmd -E -S .\SQLEXPRESS -d WEDFUTtest -Q"select HAS_PERMS_BY_NAME('sp_DBMaintenance', 'OBJECT', 'EXECUTE')" -o %FILENAMEANDPATH%
  </code></pre>
  
  <p>If I edit the batch file (so it maintains the same permissions etc) with the following command:</p>
  
  <pre><code>sqlcmd -S .\SQLEXPRESS -E -Q "EXEC sp_DBMaintenance @dbName=TestDB, @FragmentationThresholdForReorganizeTableLowerLimit='5.0', @FragmentationThresholdForRebuildTableLowerLimit='30.0'" -d TestDB -o %FILENAMEANDPATH%
  </code></pre>
  
  <p>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... </p>
  
  <p>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.</p>
  
  <p>So the question is, why isn't the 2nd command working correctly?</p>
  
  <p>P.S. %FILENAMEANDPATH% is of the form SP_Log_23-11-2016_11-41-00.log</p>