2
votes

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.

2
For a more secure approach, runas can cache passwords... but you need to enter them at least once, and the password is stored in the current user's profile (i.e.: may not be suitable). - Simon Catlin
The DBuser profile is created while the DB is installed and not used otherwise. Also, in order to install the DB from a batch file with no user interaction, I need to automate the password enter process.. - WinSupp
When I messagebox out your run line (using cat.txt as the file name) this is what I get. 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
@phd443322 \" is properly used to surround the argument of a command passed to RunAs. Check the RunAs help docs. - Bond
@user3669651 You said you can't use third party files. So does that mean other Windows apps are possible? If so, you can schedule a one-time task with SCHTASKS and supply the password as part of the command line. No SendKeys necessary. - Bond

2 Answers

0
votes

First of all, i do not think it is best to use vbscript to send a password. For example if the user were to shift focus to another window, the password would be sent to that window instead. Probably the reason why it does not work on some servers is because another window might have become the active window while the script was running.

If automation is truly needed, it might be better to use AUTOIT. However, with that being said, another method would be to enter this command in a shortcut file.

runas /profile /env /savecred /user:username pathtofile. Therefore you enter the password manually once and it will always execute as the indicated username afterwards.

0
votes
Option explicit
Dim oShell
Const password = "AdministratorPassword"
Const DBUser="localhost\administrator"
set oShell= Wscript.CreateObject("WScript.Shell")
WScript.Sleep 500
oShell.run "cmd.exe /c  runas /noprofile /user:"&DBUser&" " & WScript.Arguments(0) &""
WScript.Sleep 2000
oShell.SendKeys (password)
WScript.Sleep 300
oShell.SendKeys"~"
Wscript.Quit

Run the script from command line to run notepad.exe as administrator

cscript.exe //nologo //b runasDBUser.vbs notepad.exe