0

I have a batch file that does a runas and automatically enters the password:

@if (@CodeSection == @Batch) @then
@echo off
start "" runas /noprofile /user:myuser cmd
CScript //nologo //E:JScript "%~F0"
goto :EOF
@end
WScript.CreateObject("WScript.Shell").SendKeys("password{ENTER}");`

(source: Automatically respond to runas from batch file)

I would like to execute commands in the new window that opens as a result of the "runas" command. Does anyone know how I might go about doing this? Thanks.

Community
  • 1
  • 1
maknel
  • 309
  • 2
  • 12

1 Answers1

0
 start "" runas /noprofile /user:myuser "cmd /k FirstCommand & SecondCommand & ThirdCommand"

See Command /? for difference between /c and /k. FirstCommand etc can be a batch file.

Also before using SendKeys use wscript.appactivate "window title" to make sure you have the correct window for sendkeys.

  • `WScript.CreateObject("WScript.Shell").appactive("")` on the line before `.sendkeys`. –  Mar 09 '16 at 03:09
  • Thank you so much. Another question, sorry. Is it possible to use a declared variable for the password? – maknel Mar 11 '16 at 03:22
  • No. You have to type it. `/savecred` saves the password so you only type it once, but then anyone can start any program as admin. –  Mar 11 '16 at 08:31