129. SQL Server Script to Enable and Disable XP_CMDSHELL Configuration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-- First, Check XP_CMDSHELL is enabled on the server:
SELECT CONVERT(INT, ISNULL(value, value_in_use)) AS ConfigValue
FROM sys.configurations
WHERE name = 'xp_cmdshell' ;
 
 
-- Script to enable and disable XP_CMDSHELL:
  
-- Enable advanced options to be changed.
EXEC SP_CONFIGURE 'show advanced options', 1
GO
RECONFIGURE
GO
-- Enable xp_cmdshell option.
EXEC SP_CONFIGURE N'xp_cmdshell', 1
GO
RECONFIGURE
GO
-- Disable xp_cmdshell option.
EXEC SP_CONFIGURE 'xp_cmdshell', 0
GO
RECONFIGURE
GO
-- Disable advanced options to be changed.
EXEC SP_CONFIGURE 'show advanced options', 0
GO
RECONFIGURE
GO