I am facing some issue with a VBScript which automatically enters the password to Runas command. I am using the following VBScript (runasDBUser.vbs) to execute a program:
Option explicit
Dim oShell, k
Const PASSWORD = "DBPassword1~"
set oShell= Wscript.CreateObject("WScript.Shell")
WScript.Sleep 500
oShell.run("RunAs /noprofile /user:DBUser " & Chr(34) & "cmd /c\" & Chr(34) & WScript.Arguments(0) & "\" & Chr(34) & Chr(34))
WScript.Sleep 700
oShell.SendKeys (PASSWORD)
Wscript.Quit
This script is called in a batch file:
cscript //Nologo //B runasDBUser.vbs ""C:postgresql\bin\initdb.exe" --locale=C --encoding=UTF-8 -U %ADMIN_DB% -D "%DATA_DIR%""`
This works successfully in some of the Windows servers, but on some, it does not work. Seems like it would not enter the password correctly, and so, the initdb.exe will not run. When I manually enter the password during installation, it works fine. Is there an alternate to sendkeys which I can use to auto-enter the DBUser's password while running the "Runas" command?
Or, is there some other script which someone can suggest which would enter the Runas password automatically? Cannot use third part exe files like psexec.
RunAs /noprofile /user:DBUser "cmd /c\"cat.txt\"". Also things that don't return values don't have brackets. Backslash is not an escape in basic (doesn't have one) or batch (^). - phd443322\"is properly used to surround the argument of a command passed toRunAs. Check theRunAshelp docs. - BondSCHTASKSand supply the password as part of the command line. NoSendKeysnecessary. - Bond