viernes, 4 de marzo de 2011

xp_cmdshell: comprobar estado antes de ejecutarla

Por razones obvias, es mejor tener xp_cmdshell deshabilitado y habilitarlo sólo cuando es necesario. Para evitar males mayores, igual es recomendable antes de reconfigurar verificar su estado, y una vez ejecutado nuestro script dejarlo como estaba.

Unas pequeñas líneas pueden ahorrarnos algún que otro lío (experiencia personal esta semana :-)

Podéis crear una variable o una pequeña tabla temporal:
-- xp_cmdshell configuration --

CREATE TABLE #config (val int)

INSERT INTO #config SELECT convert(int, vlaue_in_use)
FROM master.sys.configurations WHERE name= 'xp_cmdshell'


IF (SELECT val FROM #config)= 0

-- enable xp_cmdshell and lock it again after completed the script

BEGIN
 EXEC sp_configure 'show advanced options', 1
 RECONFIGURE
 EXEC sp_configure 'xp_cmdshell',1
 RECONFIGURE

END

--AQUI EL CÓDIGO


-- set xp_cmdshell as its previous state if it was changed

IF (SELECT val FROM  #config) = 0

BEGIN
 EXEC sp_configure 'show advanced options', 1
 RECONFIGURE
 EXEC sp_configure 'xp_cmdshell',0
 RECONFIGURE
END


DROP TABLE #config

Lo mismo para OLE Automatation