Search code examples
xmlxp-cmdshell

call xp_cmdshell with xml parameter


It was created console application that has two param: one is a command like "send" and second is a xml string like "<Messages><msg>My message</msg></Messages>". Console application will return something like: "<Messages><msg>Handled message</msg></Messages>" after execution.

The question is how to call stored procedure xp_cmdshell mentioned above console application using T-SQL. The main problem is how to send parameter with xml. For example, mentioned above console application, can be executed with this expression:

"c:\consoleapp.exe" send "<Messages><msg>My message</msg></Messages>"

This expression can be executed without problem from console, but not with xp_cmdshell. In the case of xp_cmdshell sql server outut will return:

< was unexpected at this time.

Solution

  • The answer is that script:

    EXEC master.dbo.xp_cmdshell '""D:\ConsoleTest.exe" "<Messages><msg>My message</msg></Messages>""'