1

Dears ,

i am trying to make the mapped drive visible to SQL server and as a security requirement, I need to use user name and encrypted password because this will be saved on the logs if it is plain text.

EXEC xp_cmdshell 'NET USE F: \\DefinitelyNotMyLaptop\SQLBackups/ user:SqlUser P@$$w0rd'

i tried to declare a variable and save the .txt password on it but it did not work

any ideas?

  • SQL server enterprise edition ,2019
3
  • Placing passwords in shell commands or environment variables is generally considered bad practice, as there's no way to completely prevent them from appearing - as you note - in logs or to other users who can see the state of your shell, and no way to encrypt them for use that isn't easily undone. Additionally, it doesn't appear that the NET USE command allows for any form of encryption, so the network drive in question should be persistently mounted (from Windows explorer, not from cmd shell) using credentials stored and secured by the Windows OS rather than mounted at runtime.
    – pmdba
    Commented Jan 1, 2023 at 12:43
  • "i am trying to make the mapped drive visible to SQL server" - Why are you using xp_cmdshell to do this, as opposed to a standard way in Windows?
    – J.D.
    Commented Jan 1, 2023 at 16:15
  • this command is used on SQL server (SSMS) to make the drive visible when i am taking BK if i don't use this command even if the mapped drive appears on OS level it will not appear on SQL server level.
    – Tala
    Commented Jan 2, 2023 at 7:25

1 Answer 1

1

use cmdkey to install the credential ahead of time in the Windows Crediential store for the SQL Server service account, eg:

exec xp_cmdshell 'cmdkey /add:myserver /user:myserver\sqluser /pass:P@$$w0rd'

Then omit the password and user from the NET USE command.

exec xp_cmdshell 'NET USE f: \\myserver\share'

Or skip the drive mapping and backup to a UNC path, eg \\myserver\share\mydatabase.bak.

And in a domain environment you should just grant access to the share for the SQL Server Service account (or the machine account if using the default per-service SID).

1
  • thanks for your reply, but unfortunately it did not work. i received (CMDKEY: Credentials cannot be saved from this logon session.) and for the next command (Enter the user name for IP ) any idea?
    – Tala
    Commented Jan 3, 2023 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.