0

I'm making a script to launch SQL Server Management Studio using runas command on Windows machine. When ran from cmd the command prompts user to enter the password and then starts the program.

from subprocess import Popen, PIPE

user = '/user:some_domain\\username'  # \ to escape special character
path = 'C:/Program Files (x86)/Microsoft SQL Server/130/Tools/Binn/ManagementStudio/Ssms.exe'

process = Popen(['runas', '/netonly', user, path], stdin=PIPE, stdout=PIPE)  # launches 'ssms.exe'
process.communicate('password\n')  # never hit

In my case as soon as it hits the process = Popen(...) line it launches the SSMS without ever prompting for the password. Checking process in Windows Task Manager shows the same username in both cases, so I don't know if it's running on the correct domain. How can I address this?

tripleee
  • 175,061
  • 34
  • 275
  • 318
Yury Stanev
  • 37
  • 1
  • 10
  • runas.exe will only read the password from the console. Your options are to write it to the console input buffer via `WriteConsoleInput` or save the password in the user's credential vault by using the `/savecred` option (requires interactive password entry the first time). – Eryk Sun Jul 31 '18 at 18:34
  • @eryksun Unfortunately `/savecred` isn't an option as after trying it I recieve a message about trust issues. Would you mind giving an example of `WriteConsoleInput` as I don't see any in [here](https://learn.microsoft.com/en-us/windows/console/writeconsoleinput#requirements). I need to have `/netonly` as I'm accessing a machine on different domain. – Yury Stanev Jul 31 '18 at 19:05
  • See [this answer](https://stackoverflow.com/a/44056313/205580) and my comments on it. – Eryk Sun Jul 31 '18 at 19:16
  • @eryksun `psexec` doesn't work for me as I don't need to run the process on a remote machine. I need to launch `SSMS` on local machine & connect to DB. I've tried to launch `cmd` and the send in the command an input. `process = Popen(['cmd.exe', stdin=PIPE, stdout=PIPE])` and `process.communicate(input='runas ...')`, but that didn't seem to do anything useful. – Yury Stanev Aug 01 '18 at 13:44
  • Check again. I linked to the answer written by Jean-François Fabre, which shows how to use `WriteConsoleInput` in Python. – Eryk Sun Aug 01 '18 at 13:54

0 Answers0