0

I followed a few examples here, but I still can not get my sqlcmd work after runas.

I can make it work with two steps:

  1. Step one: Use runas to login into a new login and open a command prompt:

    runas.exe /savecred /user:DOMAIN_NAME\login_name cmd.exe

  2. Step two: execute the sqlcmd in a script

    sqlcmd -S server_name -E /Q"exit(SELECT @@version )"

But I want to make it one step to get the results. I tried to add " " after the runas command as listed below, but it did not work:

runas.exe /savecred  /user:DOMAIN_NAME\login_name "sqlcmd -S server_name -E /Q"exit(SELECT @@version )""

Any ideas?

dave
  • 296
  • 1
  • 12
  • 26

1 Answers1

2

Take a look at this article describing RunAs. Toward the end of the article, he specifically addresses needing to use quotes inside of quotes:

Fortunately, it’s fairly easy to make RunAs happy. All you need to do is use the \ character to “escape” any double quote marks that must to be embedded within the process path.

So, it looks like your command should be:

runas.exe /savecred  /user:DOMAIN_NAME\login_name "sqlcmd -S server_name -E /Q\"exit(SELECT @@version )\""

Notice the \ before both of the nested double quotes.

Ryan
  • 2,948
  • 3
  • 30
  • 41