SQL SERVER – Enable xp_cmdshell using sp_configure

reference : 

http://sqltidbits.com/scripts/check-if-xpcmdshell-enabled-across-multiple-servers

http://blog.sqlauthority.com/2007/04/26/sql-server-enable-xp_cmdshell-using-sp_configure/

http://www.maxxxie.net/2012/07/08/xp_cmdshell-some-more-evil/


1. How to enable & check enable status of  xp_cmdshell command 

--- 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

---to Check the enabled status 
SELECT name AS [Configuration], CONVERT(INT, ISNULL(value, value_in_use)) AS [IsEnabled]
FROM  master.sys.configurations

WHERE  name = 'xp_cmdshell'


2.Sample to use xp_cmdshell  to list down file in local system : 

CREATE TABLE ##SQLFiles ( SQLFileName VARCHAR(2000))
GO

INSERT INTO ##SQLFiles
EXECUTE master.dbo.xp_cmdshell 'dir /b "D:\Working\===onGoing\==Change Request\==Oct 7 release\File Import\DB\==SP\*.sql"'
GO

DECLARE cFiles CURSOR LOCAL FOR
    SELECT DISTINCT [SQLFileName]
    FROM ##SQLFiles
    WHERE [SQLFileName] IS NOT NULL AND
          [SQLFileName] != 'NULL'
    ORDER BY [SQLFileName]


DECLARE @vFileName         VARCHAR(200)

OPEN cFiles
FETCH NEXT FROM cFiles INTO @vFileName
WHILE @@FETCH_STATUS = 0
BEGIN
     PRINT ' @vFileName ' + @vFileName
     FETCH NEXT FROM cFiles INTO @vFileName
END

CLOSE cFiles
DEALLOCATE cFiles
GO


DROP TABLE ##SQLFiles
GO


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值