As of May 31, 2023, we have updated our Code of Conduct.

Questions tagged [xp-cmdshell]

xp_cmdshell is a Microsoft SQL Server extended stored procedure one can use to spawn a windows process to execute a command line. Questions in this tag should be about using this procedure.

Filter by
Sorted by
Tagged with
0 votes
1 answer
204 views

using encrypted password in xp_cmdshell command

Dears , i am trying to make the mapped drive visible to SQL server and as a security requirement, I need to use user name and encrypted password because this will be saved on the logs if it is plain ...
Tala 's user avatar
  • 31
0 votes
1 answer
441 views

SQL Server is not reading environment variable

I want to create a CLR assembly, and here's my code: use [master] go exec sp_configure 'show advanced options', 1 go reconfigure go exec sp_configure 'xp_cmdshell', 1 go reconfigure go execute ...
Ali EXE's user avatar
  • 179
-1 votes
1 answer
309 views

cmdshell query Error "not a valid identifier"

I've included the entire code USE [JCIHistorianADX2New] GO /****** Object: StoredProcedure [dbo].[GetADX2PointsDyn] Script Date: 10/18/2021 5:45:38 AM ******/ SET ANSI_NULLS ON GO SET ...
gunterl's user avatar
2 votes
0 answers
94 views

SQL Server - Holistic approach to managing permissions from a domain [closed]

We are small IT department and we are changing how our infrastructure works. We are trying to make it more secure, manageable and transparent for auditing. We are implementing a domain for our servers ...
AnJ's user avatar
  • 141
0 votes
1 answer
1k views

Powershell script executed by SQL Server doesn't take effect

I'm doing some testing and I want to execute a PowerShell script through xp_cmdshell. Below is the PowerShell script (c:\temp\sqltotext.ps1). echo "haha" | Out-File "c:\temp\haha.txt&...
Just a learner's user avatar
0 votes
1 answer
962 views

How to use BCP Utility queryout in a network path with crededential (login required) for SQL Server

I'm having a hard time figuring out on how can I export my selected table in a network path that requires login credentials. 'bcp "select ''StudentName'' UNION ALL SELECT StudentName AS ...
Lawless16's user avatar
1 vote
0 answers
1k views

Is it possible to use OAUTH2 in MS SQL Server

I am using msdb.sp_senddbmail procedure to send emails from MS SQL Server but now I need to move to OAUTH2. I tried to implement separate application and call it from procedures using xp_cmdshell but ...
1110's user avatar
  • 111
0 votes
2 answers
595 views

Copy last modified files from network path into SQL server box and rename them using Agent job

I have to copy latest backup from a network path and bring that to my SQL server physical drive for restore, I have to do that using agent job. This is what i am doing. Created a SQL server agent ...
datalearner's user avatar
1 vote
2 answers
3k views

SQL Server works slow after enable XP_CMDShell even disable until restart service

2 days ago I created a function that needs INSERT and UPDATE, and I enabled XP_CMDShell and do it by running a script. Afterwards, SQL Server works very slow for SELECT commands. Even very simple ...
Sayed Abolfazl Fatemi's user avatar
5 votes
2 answers
3k views

xp_cmdshell called from procedure WITH EXECUTE AS OWNER

I am trying to understand details of wrapping xp_cmdshell functionality in user defined stored procs, so that other users can just be given execute permission to the stored procs rather than ...
John Rees's user avatar
  • 206
1 vote
1 answer
174 views

Cannot take backup into NAS storage at specific time

I have a job which runs every 30 minute and takes t-log backup of the database suing Ola's script. Every Thursday and Friday at 22:45 I get an error: 11/07/2019 22:45:00,Backup,Unknown,Error: 3041<...
Rauf Asadov's user avatar
  • 1,233
0 votes
1 answer
2k views

error 15466 An error during decryption

I have a stored procedure that runs xp_cmdshell with a user with sysadmin right.(it was created by someone else long time ago). For security purposes I need to remove the sysadmin right from this user....
user183067's user avatar
0 votes
0 answers
2k views

SQL Server 2016: Run xp_cmdshell as a different sysadmin user aside from the SQL Server service account

I been trying to research this for hours and can't seem to find anything. Basically what I am trying to do is run a xp_cmdshell command as a currently signed in sysadmin user To give some background, ...
user avatar
0 votes
2 answers
1k views

Copy files twice using xp_cmdshell

When I run this command exec xp_cmdshell 'copy C:\---\cmdshell1\a23.txt C:\---\cmdshell2'; It copies a23.txt file from cmdshell1 to cmdshell2 folder. When I run the same command second time it does ...
Eola Giba's user avatar
3 votes
2 answers
8k views

how to get the date of a file using xp_cmdshell?

So far I've this code CREATE TABLE #tmp (strData VARCHAR(1000)) INSERT INTO #tmp EXEC xp_cmdshell 'dir C:\Users\Giba\Desktop\Folder\cmdshell1\*.txt' SELECT * FROM #tmp --WHERE strData LIKE '09/25/...
Eola Giba's user avatar
1 vote
1 answer
134 views

Use T-SQL to return free space on a network share

I'd like to be able to calculate the amount of free space I have for offsite backups so I can prevent any jobs from running should there not be the relevant space and also be able to send alerts when ...
Krishn's user avatar
  • 343
1 vote
0 answers
947 views

Viewing directory of a mapped drive

I'm using the following query to view files/folders in a given directory. It works locally, but when trying to view a mapped drive I get... "The system cannot find the path specified." I have the ...
duckfeet23's user avatar
1 vote
1 answer
469 views

Connection is Using Properly or not in Net Use via SQL Procedure

am doing an operation of creating a file and writing in a SAN SAMBA where it will be done using the cmd_shell via SQL Procedure. It's creating and writing it successfully but while am calling that ...
Manu C Rajan's user avatar
5 votes
3 answers
3k views

Invoke SQLCMD doesn't work when used with xp_cmdshell

I am trying to get disk space report from bunch of servers and insert them into sql table.. below is sample of what i am trying to do,Server names will be populated from a table and passed as a ...
TheGameiswar's user avatar
  • 2,929
1 vote
0 answers
122 views

xp_cmdshell - Enablement Consequences - Clarifications

Recently we have disabled a module which is dependent on xp_cmdshell. Though we are not directly using the xp_cmdshell (instead we are using the proxy), we would now like to disable the xp_cmdshell ...
Learning_Learning's user avatar
1 vote
1 answer
625 views

xp_cmdshell Date Parameter - Looks for File create Date of last modify date? [closed]

While deleting files using xp_cmdshell, does the date column look for file creation date or last modified date?
Vijred's user avatar
  • 447
0 votes
1 answer
249 views

DAC Connection using the SSMS for executing the code

Can anyone help me on below . I am trying to execute the SQLCMD with DAC (-A switch) but unable to do so . How can I do it both ways below : Execute in SSMS Query Pane Execute using PowerShell ...
Learning_Learning's user avatar
1 vote
1 answer
1k views

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 ...
Eluros's user avatar
  • 75
0 votes
2 answers
985 views

String replacement in SQLCMD

I'm trying to set up a test environment that simulates (very roughly) having three servers. I'm doing this by creating 3 databases on my local instance of SQL which I'll call [A], [B], and [C]. Why I ...
Xedni's user avatar
  • 121
5 votes
2 answers
1k views

How can I save the results of DBCC SHRINKFILE into a table?

I have the following script: -- available space in each of the database files PRINT @@SERVERNAME --SQLDWDEV01 USE [BodenStage] GO SELECT name ,(CAST(ROUND((size/128.0 - CAST(FILEPROPERTY(name, '...
Marcello Miorelli's user avatar
0 votes
2 answers
424 views

xp_cmdshell only sends first character of string

I am trying to use xp_cmdshell to launch a python script with a list as its input but for some reason its only passing the first character of the string to command line. The result I get from the code ...
clm42's user avatar
  • 5
-1 votes
2 answers
966 views

execute un file via xp_cmdshell

my text file cmdftp_put.txt contain the following: open ftp.xxx.com loginxx pwdxx put C:\Mydatabase.bak /Sources/Mydatabase.bak quit I want to execute this file through an xp_cmdshell in order to ...
user3569267's user avatar
3 votes
3 answers
3k views

Is it safe to set xp_cmdshell value 1?

I need to use xp_cmdshell in one of SQL job.So is it safe to reconfigure xp_cmdshell to 1 or can i set value 1 in the starting of SQL statements and set it back to 0 in the end of SQL Statement ?
Rehan's user avatar
  • 73
4 votes
3 answers
5k views

Execute xp_cmdshell as a non-windows user

So I have a bunch of database users who should be able to execute a procedure with a call to xp_cmdshell. They are not windows domain accounts, they are just sql-server logins. I want to do it using a ...
PacoDePaco's user avatar
0 votes
1 answer
212 views

Change the Hosts files on both SQL nodes to show 127.0.0.1 LocalHost

How to do this ? Change the Hosts files on both SQL nodes to show 127.0.0.1 LocalHost
Learning_Learning's user avatar
5 votes
6 answers
2k views

Alternative to xp_cmdshell for emailing a report as a CSV file

I've got an issue that I could do with some ideas as to how to achieve what's needed without using (or enabling) xp_cmdshell if possible. I know that xp_cmdshell itself poses risks, even with a proxy ...
Arkane's user avatar
  • 51
3 votes
1 answer
6k views

"Access is denied." for xp_cmdshell for network share

On a SQL 2014 SP 1 instance a SQL Agent job I'm using xp_cmdshell to return a list of files from a network share. This has recently stopped working and returns "Access is denied." The command is ...
tpet's user avatar
  • 1,204
4 votes
1 answer
4k views

When executing a command via xp_cmdshell what user issues the command to the system?

I'm executing the following command to stop a service on a remote server from within a stored procedure in SQL Server. ---------------- -- Stop Service ---------------- SET @Command = 'sc \\' + @...
ProfessionalAmateur's user avatar
2 votes
3 answers
11k views

CMDEXEC The process could not be created for step... (reason: Access is denied). The step failed

I have created a job to test executing a CmdExec step. The job step is simply executing a batch file with dir as the command. The job is executing as the SQL Server service account. This account ...
Don's user avatar
  • 195
11 votes
3 answers
89k views

"Unable to Open BCP host data-file" using xp_cmdshell from SSMS but command line works

I have a stored procedure that runs on a job every once in a while to BCP some files left by an application I've written. I noticed the files were piling up and BCP wasn't picking them up so I ...
Brandon's user avatar
  • 598
2 votes
1 answer
11k views

EXEC master.dbo.xp_cmdshell 'The user name or password is incorrect'

I am trying to move a file from a network drive to another server and when I execute this statement. I get an output of The user name or password is incorrect DECLARE @SourceFile AS VARCHAR(500); ...
James Youkhanis's user avatar
1 vote
3 answers
7k views

Using Echo statement writes Echo is ON in the text file

Following code is used for write a number in the text file but instead it is writing "Echo is ON" into the text file. SET @CMD = 'ECHO '+@Ver+'>'+@Vercheck Print @CMD EXEC xp_cmdshell @...
Sudheer Muthusamy's user avatar
1 vote
4 answers
2k views

UNION xp_cmdshell

I'm trying and failing to do something a follows select 1 union xp_cmdshell 'whoami' Is there anyway to get this work? The simpler the better i.e. one statement.
pee2pee's user avatar
  • 194
0 votes
1 answer
422 views

SQL Server DeleteFolder Job ran successfully but it doesn't deleted the folder

I have a job to delete past 15 days folder in SQL Server. The job run successfully and delete the folder in local SQL Server. But it runs successfully and does not delete the folder in another SQL ...
San's user avatar
  • 11
7 votes
2 answers
1k views

Why is script with " xp_cmdshell " in comment failing with a transport level error?

When executing the following batch that is part of a large script: ... GO ----------------------------------------------------------------------------------------------- -- Removed the xp_cmdshell ...
Ed Greaves's user avatar
6 votes
2 answers
3k views

problem running xp_cmdshell when Application Name set in connection string

I have an app on 10 or so servers that runs a few xp_cmdshell statements against MSSQL 2008. It works fine on all the servers except for one. To make things worse, I can run all the commands in SQL ...
John Petrak's user avatar
6 votes
2 answers
8k views

Can we minimise the risk of using xp_cmdshell?

Using xp_cmdshell can be quite helpful and sometimes possibly the only answer to some scenarios. I've read some posts on the internet that enabling xp_cmdshell might jeopardize the security of the ...
Sky's user avatar
  • 3,674