sqlsql-serverxp-cmdshellnet-use

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:

Here are my questions:

  1. Am I missing some step?
  2. Why is the connection getting unavailable after the first connect?
  3. 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! :)


Solution

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