How to enable and disable xp_cmdshell in SQL

Source: Internet
Author: User

Xp_mongoshell

-- The xp_cmdshell option introduced in SQL Server 2005 is the Server configuration option, which enables the system administrator to control whether the xp_cmdshell extension stored procedure can be executed on the system. By default, the xp_cmdshell option is disabled on the newly installed software, but you can enable it by using the peripheral application configurator tool or running the sp_configure system stored procedure, the following code example is as follows:

The code is as follows: Copy code

-- To allow advanced options to be changed.
-- Allow advanced options to be changed
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
-- Update the current configuration value as an advanced option
RECONFIGURE
GO
-- To enable the feature.
-- Enable this feature
EXEC sp_configure 'XP _ your shell', 1
GO
-- To update the currently configured value for this feature.
-- Update the current configuration value to this feature.
RECONFIGURE
GO


Another method is sp_xp_cmdshell to enable and disable xp_cmdshell instances.
 

The code is as follows: Copy code
[SQL]
USE master
GO
IF OBJECT_ID ('sp _ xp_cmdshell ', 'P') IS NOT NULL
Drop proc sp_xp_cmdshell
GO
Create procedure sp_xp_mongoshell @ OnOff CHAR (3) = 'on'
AS
/*
Author: Chen Enhui-Hong En
Example:
Exec sp_xp_cmdshell @ OnOff = 'on' -- enable the xp_cmdshell function
Exec sp_xp_cmdshell @ OnOff = 'off' -- disable xp_cmdshell.
*/
If upper (@ OnOff) not in ('on', 'off ')
BEGIN
SELECT 'parameter @ OnOff can only be ON, Off' AS return_result
RETURN
END
If upper (@ OnOff) = 'on'
BEGIN
-- Allow advanced configuration options
EXEC master. sys. sp_configure 'show advanced options', 1
-- Reconfigure
RECONFIGURE
-- Enable xp_cmdshell
EXEC master. sys. sp_configure 'XP _ your shell', 1
-- Reconfigure
RECONFIGURE
END
ELSE
BEGIN
-- Disable xp_cmdshell
EXEC master. sys. sp_configure 'XP _ your shell', 0
-- Reconfigure
RECONFIGURE
-- Disable advanced configuration options
EXEC master. sys. sp_configure 'show advanced options', 0
-- Reconfigure
RECONFIGURE
END
GO
EXEC sp_MS_marksystemobject 'sp _ xp_cmdshell'
GO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.