Using xp_cmdshell

  • I am not sure that there is any safe way to "Kill" the cmd.exe. I really think that you need to fix your Task.exe so that it works correctly as a command-line console application.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • ... and, like I suggested before, please tell us what Task.exe actually does. There may be no need for such computational shenanigans at all. 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Angel (1/9/2004)


    Yes.. Haidong..

    I wasn't in my 5 sense.. jejeje...i was complicating my life.. jejeje.. The EXE file.. was running..(Not Calc.exe... myApplication)but i want to see it.. to believe .. jejeje..

    Tnks.. for your message..

    Hi,

    Can you please tell how your application is working?

    I have a doubt. I'm trying to run an exe file which is on the other server.

    How to write the stored procedure to run the exe file?

    Thank you.

  • I have some more doubts.

    Actually I need to run an exe file from stored proc.

    So, I wrote the stored proc like:

    create proc callExe

    As

    EXEC xp_cmdshell '\\192.10.10.245\E:\PrintToPDFConsole.exe'

    In this 192.10.10.245 is another system apart from the sql server.

    and the exe file path is E:\PrintToPDFConsole.exe

    But the exe file is not working when I executed the stored proc callExe.

    Note that the PrintToPDFConsole.exe don't have any interface.

    Please help in this issue.

    Thank you.

    Krushna

  • "Not working" doesn't tell us very much. Is there an error message?

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • krushnasahu (3/26/2009)


    I have some more doubts.

    Actually I need to run an exe file from stored proc.

    So, I wrote the stored proc like:

    create proc callExe

    As

    EXEC xp_cmdshell '\\192.10.10.245\E:\PrintToPDFConsole.exe'

    In this 192.10.10.245 is another system apart from the sql server.

    and the exe file path is E:\PrintToPDFConsole.exe

    But the exe file is not working when I executed the stored proc callExe.

    Note that the PrintToPDFConsole.exe don't have any interface.

    Please help in this issue.

    Thank you.

    Krushna

    Does that actually run if you try it from a real command window?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • RBarryYoung (3/26/2009)


    "Not working" doesn't tell us very much. Is there an error message?

    Dang... now I'm disappointed... I was sure that you could read minds... 😛

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Jeff Moden (3/26/2009)


    RBarryYoung (3/26/2009)


    "Not working" doesn't tell us very much. Is there an error message?

    Dang... now I'm disappointed... I was sure that you could read minds... 😛

    Well, I do have a limited range. :hehe:

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • RBarryYoung (3/26/2009)


    Jeff Moden (3/26/2009)


    RBarryYoung (3/26/2009)


    "Not working" doesn't tell us very much. Is there an error message?

    Dang... now I'm disappointed... I was sure that you could read minds... 😛

    Well, I do have a limited range. :hehe:

    Heh... I found that if I shape my tinfoil hats like really high Mohawks, I can get much better range but it also become a little oversensitive insofar as azimuth goes... :hehe:

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Jeff Moden (3/26/2009)


    RBarryYoung (3/26/2009)


    Jeff Moden (3/26/2009)


    RBarryYoung (3/26/2009)


    "Not working" doesn't tell us very much. Is there an error message?

    Dang... now I'm disappointed... I was sure that you could read minds... 😛

    Well, I do have a limited range. :hehe:

    Heh... I found that if I shape my tinfoil hats like really high Mohawks, I can get much better range but it also become a little oversensitive insofar as azimuth goes... :hehe:

    Plus, it looks silly. 😛

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • RBarryYoung (3/26/2009)


    "Not working" doesn't tell us very much. Is there an error message?

    When the stored proc was executed, the output coming as null and the exe is not processing.

  • Jeff Moden (3/26/2009)


    krushnasahu (3/26/2009)


    I have some more doubts.

    Actually I need to run an exe file from stored proc.

    So, I wrote the stored proc like:

    create proc callExe

    As

    EXEC xp_cmdshell '\\192.10.10.245\E:\PrintToPDFConsole.exe'

    In this 192.10.10.245 is another system apart from the sql server.

    and the exe file path is E:\PrintToPDFConsole.exe

    But the exe file is not working when I executed the stored proc callExe.

    Note that the PrintToPDFConsole.exe don't have any interface.

    Please help in this issue.

    Thank you.

    Krushna

    Does that actually run if you try it from a real command window?

    Yes, the exe is working fine when it run from command prompt.

    But when the stored proc was executed, the output coming as null and the exe is not processing.

  • OK, and what happens when you execute this yourself in a Query windows?:

    xp_cmdshell '\\192.10.10.245\E:\PrintToPDFConsole.exe'

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • RBarryYoung (3/28/2009)


    OK, and what happens when you execute this yourself in a Query windows?:

    xp_cmdshell '\\192.10.10.245\E:\PrintToPDFConsole.exe'

    It was showing :

    The network path was not found.

    But after I've changed it to a shared folder 'Test' .

    Now the stored proc is :

    xp_cmdshell '\\192.10.10.245\Test\PrintToPDFConsole.exe'

    Now it is showing the output:

    Access is denied.

    Note that I'm executing on SQL Server 2000.

    Also, I've tried locally.

    I've kept the exe file in C:\.

    and changed the stored proc :

    ALTER proc callExe

    As

    EXEC master.dbo.xp_cmdshell 'C:\PrintToPDFConsole.exe'

    Now the output is :

    NULL

    Unhandled Exception: System.ComponentModel.Win32Exception: No application is associated with the specified file for this operation

    at PrintToPDF.Program.Main(String[] args)

    NULL

    and the exe file did not run again.

    Please help in this. Does it need any modification or any other idea?

    Also, I've tried with common exe file like 'notepad.exe' like:

    ALTER proc callExe

    As

    EXEC master.dbo.xp_cmdshell 'notepad.exe'

    and executed the stored proc callExe, but the execution is going on and not showing any output. The execution time is more that 10 mins and going on...

    Then I forcefully closed the isqlw.exe from Task Manager.

    Is that mean exe file can't be executed in sql server 2000 using xp_cmdshell ?

  • Krushna (3/30/2009)


    RBarryYoung (3/28/2009)


    OK, and what happens when you execute this yourself in a Query windows?:

    xp_cmdshell '\\192.10.10.245\E:\PrintToPDFConsole.exe'

    It was showing :

    The network path was not found.

    But after I've changed it to a shared folder 'Test' .

    Now the stored proc is :

    xp_cmdshell '\\192.10.10.245\Test\PrintToPDFConsole.exe'

    Now it is showing the output:

    Access is denied.

    Note that I'm executing on SQL Server 2000.

    This happens because xp_cmdshell cause the SQL Server to execute a DOS command for you on the server in a (probably) unprivileged account. You would need to figure out what account it is using and then give it all of the rights neccesary to get to the share. Note that for security reasons your SysAdmin may intentionally deny this account access to the network.

    Also, I've tried locally.

    I've kept the exe file in C:\.

    and changed the stored proc :

    ALTER proc callExe

    As

    EXEC master.dbo.xp_cmdshell 'C:\PrintToPDFConsole.exe'

    Now the output is :

    NULL

    Unhandled Exception: System.ComponentModel.Win32Exception: No application is associated with the specified file for this operation

    at PrintToPDF.Program.Main(String[] args)

    NULL

    and the exe file did not run again.

    This will not work because it is going to the Server's C: drive and not your client's. You would have to copy your exe to the Server's C: drive.

    ALTER proc callExe

    As

    EXEC master.dbo.xp_cmdshell 'notepad.exe'

    and executed the stored proc callExe, but the execution is going on and not showing any output. The execution time is more that 10 mins and going on...

    Then I forcefully closed the isqlw.exe from Task Manager.

    This probably opened a Notepad window on the server's console screen. A windows command executed from DOS like this will hang until some user exits the Windows app.

    Is that mean exe file can't be executed in sql server 2000 using xp_cmdshell ?

    No, it just means that you are doing it wrong. Remember that SQL Server is a Server. When you execute commands on SQL Server, they execute on the Server, not on your Client.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 15 posts - 31 through 45 (of 53 total)

You must be logged in to reply to this topic. Login to reply