Get file details from Share Path using SSMS - sql

I am executing the below query in SQL server 2008 to get the file details from the shared path.
DECLARE #ExecString VARCHAR(255)
SELECT #ExecString = 'dir \\<HostName>\D$\DBObjects\DBObjects_v*.SQL /B'
EXEC master..xp_cmdshell #ExecString
It is error out and returning below error message
Login failure: unknown user name or bad password.
If I copy the exact query and run it to Command prompt, provides the proper result. Below is the query executed by CMD Prompt
dir \\<HostName>\D$\DBObjects\DBObjects_v*.SQL /B
Please help me how to resolve this issue.

The latest Microsoft KB patch changing UAC restrictions on the network. Due to which, unable to access the network path of host server from store server so the required files are not copying from Host server to Store server.
Disable UAC remote restrictions to fix for this issue
Add LocalAccountTokenFilterPolicy key with value 1 in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System
Refer below link for more details
https://helgeklein.com/blog/2011/08/access-denied-trying-to-connect-to-administrative-shares-on-windows-7/

Related

SQL server : mapping network drive - Insufficient system resources exist to complete the requested service

Hello I am trying to create a new plan on SQL server to backup all my database.
My goal is to backup them to a network drive thus if I do have some trouble with my server, I will be able to restore databases to other server thanks to backup present in the network drive.
When my plan is executed, I do have some error so I try to execute manually the relative query.
After some investigation, it seems even net use command doesn't work (whereas it is working and I do it from cmd)
EXEC XP_CMDSHELL 'net use Z: \\ServerName\loggin/user:loggin password'
error is
System error 1450 has occurred. Insufficient system resources exist to complete the requested service.
Beside, I do have another server where it is working so I suppose some configuration missing but can't find them
as my network drive is also accessible via FTP, I chose this way to make the job : create a batch file that run winscp and use this batch file in a SQL agent job . I need to add right to batch file to SQL Server agent account. I also need to define a credential and a proxy to be used in the job.

Unable to connect to remote shared path through xp_cmdshell

I am trying to copy a file from a remote path to my local machine using xp_cmdshell in SQL management studio.
The following are the steps i use:
1. Establish a connection
exec xp_cmdshell 'net use L: \\remoteServer\folder /user:domain\username ImPWD'
2. Re-connect
When i execute exec xp_cmdshell 'net use', the status of L is unavailable.
So I ran exec xp_cmdshell 'net use L:'
This doesn't help anyway. It still is unavailable!
3. Copy file
EXEC xp_cmdshell 'COPY L:\Fol1\SubFol2\File.xlsx C:\work\file.xlsx'
This is the error i get:
The system cannot find the file specified.
I have tried the following but nothing seems to work yet:
Found the user credentials that is used by xp_cmdshell and tried connecting to the shared path manually through windows UI and it seems to work. Therefore i concluded that there are no permission issues.EXEC xp_cmdshell 'whoami.exe'
Ran the queries as a normal user and an administrator. Both resulted in the same output.
Here are my questions:
Am I missing some step?
Why is the connection getting unavailable after the first connect?
How to delete connection? i.e When i run exec xp_cmdshell 'net use * /delete' it asks
Do you want to continue this operation? (Y/N) [N]:
I am not sure how to say 'Y' using management studio query.
Thanks for your help in advance! :)
How to delete connection?
To delete connection use:
exec xp_cmdshell 'net use * /delete /y'
(yes) will be silently passed
As for other questions: you mentioned in comment that your folders in path have spaces so you could:
rename folders
wrap with ""

SQL xp_cmdshell copy files between servers

I am trying to move all .zip in a specific folder to another folder. the source folder is located on another server, currently i am using
EXECUTE xp_cmdshell 'copy \\server1\e$\ETL\*.zip \\server2\e$\ETL\'
GO
Which is working if I am logged into both server, but the goal is to automate this process VIA sql server job agent. I have tried
EXECUTE sp_xp_cmdshell_proxy_account 'domain\useracc','pass'
GO
EXECUTE xp_cmdshell 'copy \\server1\e$\ETL\*.zip \\server2\e$\ETL\'
GO
but I am receiving the following error;
An error occurred during the execution of sp_xp_cmdshell_proxy_account. Possible reasons: the provided account was invalid or the '##xp_cmdshell_proxy_account##' credential could not be created. Error code: '0'.
And also not sure if this is my solution. Please help with how I can achieve this. The file names on server1 change name and quantity everyday.
I would strongly advise...Do not use xp_cmdshell. It opens up large security wholes in your surface area and makes you vulnerable to attack. xp_cmdshell should be disabled!
Instead, if you want to automate this with server agent you have 2 options. My preference would be to write a simple SSIS package with a file system task and schedule this package with server agent. SSIS is underutilized for this kind of task but is actually pretty good at it.
Alternatively re-write your script to use Server Agent CmdExec job steps. This does not require xp_cmdshell to be enabled and reduces the attack surface.
I Found that the following worked for me;
In the command prompt, type services.msc, this would open the list of all services on the server.
In the list of services, look for SQL Server Agent, Right Click -> Properties. Go to Logon Tab
Change the logon to a user with access on both servers. then re-write your script to use Server Agent CmdExec job steps(Thank you Pete Carter)

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 '\\IPaddress\Images' -- returns "Access is denied."
Please note that i can access the network location but not using sql server.
Sql server is running under Winodws Authentication mode.
Sql server is using 'nt authority\network service' account to access the remote Folder.
Regards
If you run xp_cmdshell 'whoami.exe' it will tell you the account the command is running under. If this account doesn't have permissions on the network, you'll get the error you are seeing.
Check the SQL documentation for changing this account/permissions.
When you have to put file, like BCP result, or a backup in a remote drive, just map this drive into windows don't work, it must be mapped on SQL Server to!, to do this, try some link like this:
exec xp_cmdshell 'net use p:\ \Server\Folder\Folder\Folder\ /Domain\Login /Password'
Reference : https://social.msdn.microsoft.com/Forums/en-US/6eca2d62-eb86-4f23-9b86-6f917017f50c/bcp-utility-via-xpcmdshell-and-network-drive?forum=sqlsecurity

bcp error while exporting data using management studio in sql 2005 express but not using DOS

i am using sql server 2005 express edition .
when i export data via bcp utility using dos prompt then there is no error ,
but when i created a stored procedure for the export process and use management studio express for exporting data then it gives the following error :
SQLState = S1000, NativeError = 0
Error = [Microsoft][SQL Server Native Client 10.0]Unable to open BCP host data-file
Please help.
I had the same issue, but the answer is that I was being less than bright.
As the command was being run on a server, not my local machine, it was trying to write to a non-existent folder on the server. It was executing successfully against the root of C: but not against my share folder, C:\share. The problem for me was that I was looking for the file in the root of my C Drive, not the servers.
When I changed the path to \mymachinename\share, everything worked.
When you run the bcp utility using the DOS command line you are using the logged in persons credentials (usually your own), but when running as a stored procedure you are using the credentials of the SQL server process, which usually is configured to have much less permissions than ordinary users in order to provide safety against various attacks.
Check in the Services list of which user is used for the SQL server database engine and check if that user has enough read/write permissions to the directories and files involved.