I'm looking to monitor various events that may occur within a SQL Server Maintenance Plan. My desired method for doing this is to have the maintenance plan do an HTTP POST with the data. After some research I think my preferred approach is to use cURL but I understand this requires enabling xp_cmdshell which is not ideal from a security standpoint.
So, I'm trying to put together a bit of T-SQL that will check if xp_cmdshell is enabled and if not enable it long enough to run the command and then disable it again. However, what I have below is not cooperating and I'm pretty sure it has something to do with all those GO's...
DECLARE #cmdstatus INT
SET #cmdstatus = (SELECT CONVERT(INT, ISNULL(value, value_in_use)) AS config_value FROM sys.configurations WHERE name = N'xp_cmdshell')
IF #cmdstatus = 1
EXEC xp_cmdshell 'curl --user user:password --data "task=test&status=success" https://www.example.ca/index.php';
GO
IF #cmdstatus = 0
EXEC sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'xp_cmdshell', 1;
GO
RECONFIGURE;
GO
EXEC xp_cmdshell 'curl --user user:password --data "task=test&status=success" https://www.example.ca/index.php';
GO
EXEC sp_configure 'xp_cmdshell', 0;
GO
RECONFIGURE;
GO
EXEC sp_configure 'show advanced options', 0;
GO
RECONFIGURE;
GO
Thanks for any help.
Related
This is code does not run in the Crystal Report.
Error:Failed to retrieve data from the database. Details: ADO Error Code 0x80040e14 Source Microsoft SQL Server Native Client 11.0 Description: Incorrect syntax near isp_configure. SQL State 42000 Native Error: 102 [Database Vendor Code: 102
Declare #ReportDate DateTime
Set #ReportDate={?ReportDateR}
Select * from [SBO_SPEL].[dbo].[U_SPEL_SALES_ANALYSIS_TARGET&ACTUAL_UNIT_F] (#ReportDate)
UNION ALL
Select * from [SBO_SPEL].[dbo].[U_SPEL_SALES_ANALYSIS_TARGET&ACTUAL_SE_F] (#ReportDate)
GO
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'max server memory', 32768;
GO
RECONFIGURE;
GO
WAITFOR DELAY '00:02:00'
GO
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'max server memory', 131072;
GO
RECONFIGURE;
GO
Crystal returns to the report the result set from the last SQL statement in a Command or SP. But your last SQL statement has no result set.
I am currently working at one of my clients SQL-Server environment. A year ago I was asked to implement a .csv report generation in a Maintenance Plan I was working on. To do so, I have enabled xp_cmdshell option on the server and used bcp utility. However, a programmer from the other company recently added a stored procedure in which he enables xp_cmdshell option at the beginning of procedure and disables it (sets to 0) at its end. This feature have caused my Maintenance Plan to fail at the report generation step.
Since I'm not allowed to change his code I was asked to include similar enabling/disabling feature in my scripts. Here is example of code I found and used:
declare #prevAdvancedOptions int;
declare #prevXpCmdshell int;
select #prevAdvancedOptions = cast(value_in_use as int) from sys.configurations where name = 'show advanced options';
select #prevXpCmdshell = cast(value_in_use as int) from sys.configurations where name = 'xp_cmdshell';
if (#prevAdvancedOptions = 0)
begin
exec sp_configure 'show advanced options', 1;
reconfigure;
end;
if (#prevXpCmdshell = 0)
begin
exec sp_configure 'xp_cmdshell', 1;
reconfigure;
end;
--- doing some work here ---
if (#prevXpCmdshell = 0)
begin
exec sp_configure 'xp_cmdshell', 0;
reconfigure;
end;
if (#prevAdvancedOptions = 0)
begin
exec sp_configure 'show advanced options', 0;
reconfigure;
end;
The thing is I'm not completly sure about the safety of this solution but I don't want to redesign the code I wrote. I am also using xp_cmdshell feature in some other scripts.
The question is: Is it possible that execution of Script A (used by my maintenance plan) can be affected with disabling xp_cmdshell in Script B (used in the stored procedure) when they are executed at the same time?
I have done some research about SQL-Server Query Processor to understand how it works and executes the queries but could not find an answer to this question.
I will appreciate any of your suggestions!
How to enable the
xp_cmdshell and why is it blocked or disabled?*
while enabling you need Check out the Permission section of the
xp_cmdshell MSDN docs:
please follow link
https://msdn.microsoft.com/en-us/library/ms190693.aspx
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO
xp_cmdshell- Executes a given command string or batch file as an operating-system command shell and returns any output as rows of text.
Permission/Rights: Only SysAdmin fixed role can execute it.
Syntax
xp_cmdshell {‘command_string‘} [, no_output]
Arguments
‘command_string‘
Is the command string to execute at the operating-system command shell or from DOS prompt. command_string is varchar(255) or nvarchar(4000), with no default.
command_string cannot contain more than one set of double quotation marks.
A single pair of quotation marks is necessary if any spaces are present in the file paths or program names referenced by command_string.
If you have trouble with embedded spaces, consider using FAT 8.3 file names as a workaround.
no_output
Is an optional parameter executi
-- 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
-- Disable xp_cmdshell option now
EXEC sp_configure 'xp_cmdshell', 0
GO
RECONFIGURE
GO
FOr More Info-- http://yrushka.com/index.php/sql-server/security/execute-remotely-t-sql-command-through-xp_cmdshell/
Use Master
GO
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
GO
I want to export values from a column (TcpIpAddress) from a table called dbo.DimServere to a plain text (located in the server). I have sysadmin rights.
-- 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; -- 1 for at enable
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
-- Extracting information from the databse
EXEC xp_cmdshell 'bcp "SELECT TcpIpAddress FROM [SIT-DVH].[dbo].[DimServere]" queryout "C:\Users\b013904\Desktop\Output\bcptest.txt" -T -c -t,'
-- 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 disable the feature.
EXEC sp_configure 'xp_cmdshell', 0; -- 0 for at disable
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
However, when I run this script, I get the following message and no file is been created:
What am I doing wrong?
The path in that bcp statement will be relative to the server since you're executing it on the server.
Does that path exist on the server?
Also, try changing the path to something more accessible like c:\output. .. then you can play around with the permissions on that folder to ensure that is not a os permission that's causing the statement to fail.
Hope that helps
Looking for Enabling CLR Integration I found this document: http://msdn.microsoft.com/en-us/library/ms131048.aspx that said to use the following code for setting to 1 the "crl enabled" variable.
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'clr enabled', 1;
GO
RECONFIGURE;
GO
I want know If a reboot of SQL Server is required? Or, more generaly, what are the steps to follow in order to Enable CRL Integration?
If you use with override option, then restart is not required.
EXEC sp_CONFIGURE 'show advanced options' , '1';
GO
RECONFIGURE WITH OVERRIDE
GO
EXEC sp_CONFIGURE 'clr enabled' , '1'
GO
RECONFIGURE WITH OVERRIDE
GO
The accepted answer is incorrect. The WITH OVERRIDE option of RECONFIGURE has absolutely nothing to do with whether or not a restart of SQL Server is required. The MSDN documentation for RECONFIGURE states that WITH OVERRIDE:
Disables the configuration value checking (for values that are not valid or for nonrecommended values)...
The fact is, no restart of the SQL Server service is required when enabling, or disabling, the "CLR Integration" option in sp_configure. A simple test (run on SQL Server 2008 R2, but works the same across all versions that support SQLCLR) proves this:
EXEC sp_configure 'clr enabled'; -- show current value
EXEC sp_configure 'clr enabled', 0; RECONFIGURE;
EXEC sp_configure 'clr enabled'; -- show current value
GO
EXEC sp_configure 'clr enabled'; -- show current value
EXEC sp_configure 'clr enabled', 1; RECONFIGURE;
EXEC sp_configure 'clr enabled'; -- show current value
GO
Results:
Pay attention to the run_value field. It starts out as "1" since "CLR Integration" is already enabled on my system. But it switches with only calling RECONFIGURE.
name minimum maximum config_value run_value
clr enabled 0 1 1 1
clr enabled 0 1 0 0
clr enabled 0 1 0 0
clr enabled 0 1 1 1
Additionally, it should be stated with regards to the initial code shown in the Question, the statement for
sp_configure 'show advanced options', 1;
is unnecessary since clr enabled is not an advanced option.
To prove the point about clr enabled not being an advanced option, and even showing another way to prove that this option does not require a reboot, just execute the following simple query:
SELECT [name], [value], [value_in_use], [is_dynamic], [is_advanced]
FROM sys.configurations
WHERE [configuration_id] = 1562;
/*
name value value_in_use is_dynamic is_advanced
clr enabled 1 1 1 0
*/
As you can see in the result set shown above, is_advanced is 0, meaning "not an advanced option (yes, the official Microsoft documentation is currently incorrect; I will update it when I have time). Also, is_dynamic is 1, meaning that simply executing RECONFIGURE will enable the option immediately, not requiring a restart of the instance.
To summarize: The sum total of all steps required to enable "CLR Integration", and without needing to restart the SQL Server service, are as follows:
EXEC sp_configure 'clr enabled', 1;
RECONFIGURE;
That's it. **
** WOW64 servers will require a restart of the server in order for this option to take effect. ( clr enabled Server Configuration Option )