Unavailable Shared Path using net use via xp_cmdshell

347 Views Asked by At

I have been trying to resolve this really weird issue for a while!

We have a stored procedure that performs the below (all executed via xp_cmdshell which is run using the proxy account that is set to the service account)

  1. exec xp_cmdshell 'net use /delete X:'
  2. exec xp_cmdshell 'net use X: \\<server_ip>\FOLDER
  3. exec xp_cmdshell 'X:\SomeExeFile.exe'

However, when running step 3, I am getting a 'The system cannot find the drive specified'. Running exec xp_cmdshell 'whoami' between steps 2 and 3 gives me the service account user, but running exec xp_cmdshell 'net use' between steps 2 and 3 states that X: is 'Unavailable' !

Moreover, at this point, running 'net use' from a cmd window (which is run as the service account user) also gives 'Unavailable' but when I execute all the above steps 1-3 again from the cmd window, everything works fine.

(And to further add, running exec xp_cmdshell 'net use' from SSMS after doing the above via cmd then gives 'Disconnected X:' )

I cannot seem to understand what is happening since I am under the impression that net use executed on cmd and net use executed via xp_cmdshell should be the same. Can anyone help me understand why running 'net use' via xp_cmdshell just after creating it using same user is giving 'Unavailable' please?

Thanks in advance!

PS: what's weird is that when I run the above from SSMS connected with a sysadmin account, this issue is not present!

1

There are 1 best solutions below

0
siggemannen On

I suspect the problem is that after each xp_cmdshell call, the end of impersonation "kills" the share, and that's why it works to run it in one go.

As a workaround, you can put the whole script into .bat file and execute it