How to Enable xp_cmdshell Using sp_configure and Run Batch File from xp_cmdshell
Disclaimer: xp_cmdshell is a feature that is disabled by default in SQL Server, and for a very good reason. Serious consideration should be given to the potential risks and security implications which enabling this feature will expose an environment to – primarily that a compromised SQL server can (through the xp_cmdshell) allow execution of files and scripts at the operating system layer. So with this in mind…
The xp_cmdshell option is a server configuration option that enables system administrators to control whether the xp_cmdshell extended stored procedure can be executed on a system.
-- To allow advanced options to be changed. EXEC sp_configure 'show advanced options', 1 GO -- To update the currently configured value for advanced options. RECONFIGURE GO -- To enable the feature. EXEC sp_configure 'xp_cmdshell', 1 GO -- To update the currently configured value for this feature. RECONFIGURE GO
Once enabled, you can then run batch files through a SQL T-SQL via;
EXEC master..xp_CMDShell 'c:batchscript.bat'