top of page

xp_cmdshell richtig einsetzen / use xp_cmdshell correctly

(alle Versionen/all versions)

Sonntag, 2. Juli 2023

Hintergrund / Background

Beispiel Script / Example Script

Voraussetzungen / Requirements



Hintergrund / Background


Mit dem SQL-Befehl "xp_cmdshell" kann unter bestimmten Voraussetzungen direkt aus SQL-Code heraus eine Anwendung aus dem SQL-Server-Prozess heraus gestartet werden, sofern bestimmte Voraussetzungen erfüllt sind.

Der erste Parameter der Funktion ist der Pfad und Dateiname der auszuführenden Anwendung.

⚠️Falls eines der Pfad-Elemente Leerzeichen beinhaltet, so muss das einzelne Element in Anführungszeichen gesetzt werden, nicht der Pfad zur gesamten Anwendung.⚠️


Under certain conditions, the SQL command "xp_cmdshell" can be used to start an application from the SQL server process directly from the SQL code, provided certain conditions are met.

⚠️The first parameter of the function is the path and filename of the application to run.

If any of the path elements contain spaces, the individual element must be enclosed in quotation marks, not the path to the entire application.⚠️


Beispiel Script / Example Script

Falscher Pfad / wrong path:

"C:\Program Files\My Program\MyApp.exe"

Richtig / correct:

C:\"Program Files"\"My Program"\MyApp.exe

Vollständiges Beispiel / full example:


Im Beispiel ist ein möglicher Ansatz für das Fehlermanagement bzw. das Auswerten von Rückmeldungen der ausgeführten Anwendung enthalten.


The example contains a possible approach for error management/the evaluation of

feedback from the executed application.

DECLARE @lResultCode INT
DECLARE @sCommand VARCHAR(8000)
DECLARE @sCmdTool VARCHAR(255)
DECLARE @sCmdToolParam VARCHAR(4000)
DECLARE @sCommandMsg VARCHAR(MAX)
DECLARE @tblCommandResult TABLE ( [ID] int IDENTITY , [ResultText] VARCHAR(MAX) NULL )

-- TODO: Set your app path and name and also your parameters.
-- Note that the path is to be interpreted from the perspective of the SQL server, of course, and that sufficient rights are needed to access the path and the file, for example, when being executed from an SQL Agent job.

-- Note that the first parameter of "xp_cmdshell" is always the application which is to be executed.
-- Any spaces within any part of the path elements must be put in quotation marks individually!
SET @sCmdTool = 'C:\"Program Files"\"My Program"\MyApp.exe'

-- Let's assume you call your own application with parameters.
-- You can provide the parameters as you see fit.
-- The xp_cmdshell will provide the full text to your app.
SET @sCmdToolParam = '4711 "C:\Program Files\Test\Test.txt"'

SET @sCommand = @sCmdTool + ' ' + @sCmdToolParam

INSERT @tblCommandResult EXEC @lResultCode = master.dbo.xp_cmdshell @sCommand

SET @sCommandMsg = ''

SELECT @sCommandMsg = @sCommandMsg + ' ' + [ResultText] FROM @tblCommandResult WHERE NOT [ResultText] IS NULL

IF @lResultCode <> 0 
BEGIN
  
  -- TODO Your error handling!
  SELECT @sCommandMsg

END

Voraussetzungen / Requirements


⚠️Es ist zu beachten, dass der Befehl "xp_cmdshell" ausschließlich dann zur Verfügung steht, wenn dies zuvor in den Grundlagen des SQL-Servers aktiviert wurde.⚠️


⚠️Note that the "xp_cmdshell" command is only available if this has been previously enabled in SQL Server settings.⚠️


EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE

EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE

GO

bottom of page