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:
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:
""