locked
Execute batch file RRS feed

  • Question

  • Hello,

    I have a simple and short batch file I am trying to execute from SQL server. The SQL database I am connected to is my local instance. The script I am using is as below:

    EXECUTE [master].[dbo].[xp_cmdshell] 'C:\Users\Jdoe\Desktop\Test.bat'

    The batch file is located at the path listed above and contains the below:

    SchTasks /query /fo csv > I:\Schedtask.txt /v /s reports.xyz.org
     

    The above command line code connects to a remote machine and gets the list of all scheduled tasks. Please note that executing the batch file by double-clicking on the icon works without issues. When I try however to run the SQL listed above, I get the below:

    output
    NULL
    C:\Windows\system32>SchTasks /query /fo csv  /v /s reports.xyz.org 1>I:\Schedtask.txt 
    The system cannot find the path specified.
    NULL

    What am I doing wrong. I am expecting the SQL to execute de batch file which should create a output file at the specified location but it doesn't.

    Thanks for helping

    Monday, July 28, 2014 1:22 PM

Answers

  • Try to use UNC path (\\server\share\file_path). Also make sure that the doamain account used as task owner has the proper rights on the source and destination

    Javier Villegas | @javier_villhttp://sql-javier-villegas.blogspot.com/

    Please click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if a post has been useful to you

    • Proposed as answer by Elvis Long Tuesday, July 29, 2014 6:28 AM
    • Marked as answer by Elvis Long Thursday, August 7, 2014 4:19 AM
    Monday, July 28, 2014 1:33 PM

All replies

  • I guess drive I: is a mapped network share? It's mapped during Login for your user, but not for SQL Server Service account. Use the UNC path instead.

    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Monday, July 28, 2014 1:24 PM
  • Try to use UNC path (\\server\share\file_path). Also make sure that the doamain account used as task owner has the proper rights on the source and destination

    Javier Villegas | @javier_villhttp://sql-javier-villegas.blogspot.com/

    Please click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if a post has been useful to you

    • Proposed as answer by Elvis Long Tuesday, July 29, 2014 6:28 AM
    • Marked as answer by Elvis Long Thursday, August 7, 2014 4:19 AM
    Monday, July 28, 2014 1:33 PM