SQLServerCentral Editorial

Xp_cmdshell Use Cases

,

I had someone ask me recently how to run xp_cmdshell on a Linux version of SQL Server. I told them you can't, as it's an unsupported feature and not one that I expect to see released. I had to double-check, since I did think that supporting a BASH shell was a possibility, but it wasn't added to the product.

In the feedback forums, I saw a request for xp_powershell, though the feedback from MS is to use CLR for this. They suggest external access permissions, but those aren't supported on Linux. I also didn't see a request for shell scripts added, and I'm not sure I want one.

A few years ago, I wrote a piece on the dangers of xp_cmdshell, as this does create a security risk. I can be mitigated, but the modern world is complex and it can be easy to make a mistake here. I've used xp_cmdshell often without issues, but I've also known the risks and tried to mitigate them with controls on the machine, network, and who can execute the procedure.

I've seen people use xp_cmdshell for a number of tasks, like exporting a result set to a file, checking disk space, moving files after a BULK INSERT or backup, or some other task that is tightly related to actions taking in T-SQL. This can be a very handy utility for many administrators.

Today I'm curious what are your use cases. Where do you use this utility, or where is it much easier than adding a PowerShell step in an Agent job? Similarly, do you use xp_fileexist or other XPs to do things are are outside of the realm of T-SQL. Leave a comment below and let us know how you use this stored procedure. Or in which situations this has proven to be useful in the past.

And maybe vote for a BULK EXPORT 😉

Rate

5 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (1)

You rated this post out of 5. Change rating