Questions tagged [xp-cmdshell]

an extended stored procedure provided by SQL Server, which launches a Windows command shell to run a specified command. Not for general questions about cmd.exe or batch scripting; not related to Windows XP.

The xp_cmdshell extended stored procedure runs the command string given and returns any output as rows of text. The documentation (for SQL Server 2012) can be found here.

For security reasons, xp_cmdshell is disabled by default in recent versions of SQL Server.

264 questions
42
votes
4 answers

Reading a text file with SQL Server

I am trying to read in a text file from an SQL query (SQL Server 2005) but am not having any luck at all. I've tried various things with EXEC and xp_cmdshell, but all aren't working. This is the general way I've tried to approach this: CREATE TABLE…
Markus O'Reilly
  • 811
  • 3
  • 9
  • 15
37
votes
6 answers

Getting execute permission to xp_cmdshell

I am seeing an error message when trying to execute xp_cmdshell from within a stored procedure. xp_cmdshell is enabled on the instance. And the execute permission was granted to my user, but I am still seeing the exception. The EXECUTE permission…
David Griffiths
  • 640
  • 1
  • 7
  • 14
23
votes
1 answer

How do I check if a directory exists using SQL Server?

I know this command will create a directory: EXEC master.sys.xp_create_subdir 'C:\testing\' But how do I check whether 'C:\testing\' exists? IF EXISTS(...
JJ.
  • 9,580
  • 37
  • 116
  • 189
21
votes
2 answers

Get Results from XP_CMDSHELL

I have been searching the web some and it seems like the only way to get the results from XP_CMDSHELL is to store them into a temp table. Is there really no easier way? From Experts Exchange: No, xp_cmdshell will not return any information from…
Saint Ronin
  • 271
  • 1
  • 4
  • 11
15
votes
2 answers

How to delete files on the directory via MS SQL Server

I am trying to delete a file from a directory inside windows using the following query, exec xp_cmdshell 'del "C:\root\sfd_devtracker\'+@deletefile + '"'; When i execute this command it gives the following error, Incorrect syntax near '+'. In…
6
votes
3 answers

xp_cmdshell hangs after called exe has exited

I have a problem with a hang using xp_cmdshell. The executable is called, performs its work, and exits. It is not hanging because of a ui prompt in the exe. The exe is not hanging at all. The exe disappears from the process list in task manager,…
Clyde
  • 8,017
  • 11
  • 56
  • 87
6
votes
4 answers

Run a console application from SQL Server after table update trigger asynchronously?

I am having trouble with finding the best way to solve my issue, please keep in mind I am open to better ways of going about this task. What I need to do is, after a row's value in my table is updated, I need to use 2 fields from that table as…
kds6253
  • 835
  • 1
  • 12
  • 17
6
votes
4 answers

SQL Server execute xp_cmdshell to import text file over FTP protocol from a UNIX box

In the past I have used xp_cmdshell in SQL Server to invoke BCP utility for importing a text file into a table on SQL Server between two Windows machines. The two machines were on the same domain without needing a password (since the SQL server…
Angel Cloudwalker
  • 2,015
  • 5
  • 32
  • 54
6
votes
1 answer

Create zip file from SQL Server

Here is code which I use to create a .rar file with a password DECLARE @source VARCHAR(1000), @destination VARCHAR(1000), @Command VARCHAR(1000) SET @source = 'E:\Temp\testRar.txt' SET @destination = 'E:\Temp\testRar.rar' SET…
tungula
  • 578
  • 1
  • 6
  • 12
6
votes
7 answers

Access denied for enabled xp_cmdshell for the admin user

I'm using xp_cmdshell within a database trigger to launch a exe file. xp_cmdshell is enabled(it can execute simple cmd command like 'echo'). But when I try to launch the exe through xp_cmdshell, the access is denied. I am the database…
Joshua H.
  • 537
  • 3
  • 9
  • 19
5
votes
0 answers

SQL Server 2014: Using gMSA for xp_cmdshell Proxy Account?

We are running SQL Server 2014. We want to use a gMSA as the proxy account for xp_cmdshell. I tried running the following: EXEC sys.sp_xp_cmdshell_proxy_account 'gmsaNameGoesHere$', '' However, it expects a password. I tried running the…
Eluros
  • 105
  • 6
4
votes
2 answers

how can i access a file/folder over network through XP_CMDSHELL in sql server 2008?

I am trying to access a folder/directory using 'EXEC MASTER..XP_CMDSHELL' it works for the local file/folder, how ever it can not access the folder over network. EXEC MASTER..XP_CMDSHELL 'c:\Images' --Works fine EXEC MASTER..XP_CMDSHELL…
Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
4
votes
4 answers

Microsoft SQL xp_cmdshell doesn't like filenames with spaces. Can I replace the space with something else?

I have this TSQL code that dumps data from tables using BCP. It looks complicated, but it simply creates a @command string to be executed once for each table, then BCP dump the table records to disk. It's a nice way to backup all the table data…
Zachary Scott
  • 20,968
  • 35
  • 123
  • 205
4
votes
2 answers

CMD powershell command not allowing pipe

I am trying to get SQL to execute some powershell commands using xp_cmdshell which has been working, however I'm running into an unusual problem. When I try to use a pipeline, it doesn't recognize the command after the pipeline. I tried this from…
Xanderu
  • 747
  • 1
  • 8
  • 30
4
votes
2 answers

SQL 2012 bcp call returns SQLState = 28000. NativeError = 18456 Login failed for user

I’m working with a SQL stored procedure that makes a call to xp_cmdshell. xp_cmdshell has been enabled, and has a proxy account set to ‘vpexporter’. This sproc was designed to write out a data file to disk. This sproc had been working when it was…
user2015032
  • 41
  • 1
  • 1
  • 4
1
2 3
17 18