trying to understand xp_cmdShell - ssms

I know how to use xp_cmdshell to get a listing of a local directory.
The statement below gives me a list of cvs files in the dir C:\TEST.
I ran the statement in my locally installed SSMS.
EXEC Master..xp_cmdShell 'dir C:\TEST\*.csv /b'
However,
EXEC Master..xp_cmdShell 'dir \\ip-adress\share\folder\*.csv /b'
returns a 'No access'.
But running dir \\ip-adress\share\folder\*.csv /b in my commandshell does return a listing. Apparantly I as a user have sufficient rights. Then why does xp_cmdShell return a 'access denied' ?
Is xp_cmdSHell unusable with shares or has this to do with the account that issues the command? But if I started SSMS locally, should the command not run locally under my WIndowsaccount? Same as in commandshell?

I used a SSIS proxy based on my own credentials but it looks like the machine on which the SQL Server is running, is blocked from accessing that share

Related

xp_cmdshell throwing 'null' but working in command prompt

I am using xp_cmdshell command to process csv file to a folder. My query is as below:
Exec master..xp_cmdshell 'C:\test\mydotnetapp.exe -s:03/09/2020 -t:1 -l:1,2,3,4,7 -d C:\test'
In SSMS this command returns null. But in command prompt when I do:
cd C:\test
mydotnetapp.exe -s:03/09/2020 -t:1 -l:1,2,3,4,7 -d C:\test
The same query is working perfectly fine. A couple of things:
xp_cmdshell is enabled
SQL Server has access to the folder
Only this xp_cmdshell command is not working. my other xp_cmdshell is working fine
Any help on how this can be fixed?
If this is run as part of a SQL Job regularly scheduled, run it as a task outside of SQL Server.
Many production DBAs won't let any process run XP_CMDSHELL given the security issues.
If the CSV is well-formed, it can be mapped into SQL Server as a table (not recommended).
It can be imported directly into SQL Server.
Lastly, many production DBAs won't install any custom process code on a production SQL Server machine. The SQL Server machine is dedicated to running only SQL Server plus the minimum extra needed for security and monitoring.

How to create .csv file from SQL with xp_cmdshell on linux shared folder with samba?

So its kind of complicated I think, I've been searching all day for a solution.
I have a SQL Server 2008 R2 and with xp_cmdshell I currently generate some .csv files in C:\
What I want to do now is generate them directly into a linux server in the network.
I tried this :
EXEC xp_cmdshell 'NET USE T: \\192.168.0.25\PERSONS 123456 /user:linuxuser /PERSISTENT:yes'
set #filepath = 'T:'
set #command ='echo '+#customerheader+' > '+#filepath+'\PERSONS\'+convert(varchar,123456)+'.csv'
set #command = replace(#command,'&','e')
exec master..xp_cmdshell #command
EXEC xp_cmdshell 'net use T: /delete'
And what I get is: "The system cannot find the path specified."
While the mapped drive is created succesfully: "The command completed successfully." and "T: was deleted successfully."
Thank you in advance
I think you need to get away from: xp_cmdshell
What about the following?
NET USE T: \\192.168.0.25\PERSONS 123456 /user:linuxuser /PERSISTENT:yes
SET SQLSERVER=YOUR_sql_SERVER
SET SQLQUERY=C:\LOCALFOLDER\somequery.sql
SET OUTPUTFILE=T:\somefolder\someoutput.csv
sqlcmd -S %SQLSERVER% -d YOUR_DATABASE -U SQL_USER -P SQLPASSWORD -i"%SQLQUERY%" -o "%OUTPUTFILE%" -s"," -y30
net use T: /delete
I think the problem is that each time you invoke xp_cmdshell, you invoke a separate PID for CMD.exe and is a different session or instance and is causing you trouble.
Hope this helps!

How to save txt File created by BCP command on different server?

I have created text file using following command on the server where database resides
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE EXEC xp_cmdshell 'bcp "SELECT top 10 macnum, Cus_name,Cus_Email FROM [VBOS_TSP].[dbo].[Cust_file]" queryout "d:\creatFile.txt" -T -c'
But I need to create that text file on another machine. As I don't want to save that text file on production. Please suggest me other way so that by passing Path and/or passing credentials of a particular server. How to generate that file on other server?
The path in the other machine should be shared path and accessible from this machine and should have full permissions for the folder to read/write.
I worked on similar scenario and below code format works for me. Try it once after give permissions
#LV_FILE_PATH='\\192.168.1.105\Folder\creatFile.txt'
#LV_SQLTXT='"SELECT top 10 macnum, Cus_name,Cus_Email FROM [VBOS_TSP].[dbo].[Cust_file]"'
SET #LV_CMDTXT = 'BCP ' + #LV_SQLTXT + ' QUERYOUT "' + #LV_FILE_PATH + '" -c -U -T -S -r\n'
EXEC MASTER..XP_CMDSHELL #LV_CMDTXT
You can't use an unc path for queryout at bcp. You're only solution is to export it on the current machine and copy it afterwards.
Another idea is to run bcp on the remote maschine and connect to your datastore to export the data by using the parameter -S.
I hope this helps you.

Export data with bcp in SQL Server produces nothing

I am exporting data in SQL Server 2005 with the following command but it produces nothing in SQL server 2008, no file is created although the query is executed succesfully:
execute xp_cmdshell 'bcp "SELECT * FROM MYDB.dbo.MYTABLE" queryout d:\file.csv -c -t; -S(local) -T'
What is wrong with my command?
I managed to execute the command. Solution:
I installed more than one sql server versions. And the management studio cannot find the bcp.exe because in the enviroment path, there are more than one paths for the bcp.exe. You can check this by executing bcp.exe from command prompt and it will say sqlncli.dll was not found.
So you have to put the exact path of bcp.exe to the enviroment path (or in front of other paths). So that windows can use it. Then dont forget to restart the sql server in order to change in paths takes effect. In conclusion, my command is not wrong but the path of bcp.exe is wrong.

How to Execute SVN Update command through SQL xp_CMDShell

I have installed Tortoise SVN on my machine, i want to update my local copy with SVN using SVN UPDATE command. I am using SQL script
EXEC master..xp_CMDShell 'svn update D:\SVN_WorkingFolder'
This script is not executing. May i know where i am doing workng.
Thanks in advances guys.
KumaR
Script execution process won't complete because of insufficient permissions to access network.
We won't get any error message or output redirection.
Steps to give permission: Services--> SQLServer2005--> Properties-->Log on-->This account--> Provide local user credentials. Now restart SQL server then execute above SVN update command it will work. Even we can execute batch file to do this job.