1
votes

I created the following runas.vbs script:

**************
Option explicit

Dim oShell, k

Const PASSWORD = "Pass123~"

set oShell= Wscript.CreateObject("WScript.Shell")

WScript.Sleep 500

oShell.run("RunAs /noprofile /user:%computername%\postgres " & Chr(34) & "cmd /c\" & 

Chr(34) & WScript.Arguments(0) & "\" & Chr(34) & Chr(34))

WScript.Sleep 1000

For k=1 To Len(PASSWORD)

  WScript.Sleep 200

  oShell.SendKeys Mid(PASSWORD,k,1)

Next

Wscript.Quit

**************

I use this vbscript in a Batch file to run initdb.exe (Postgresql). Used as:

cscript //Nologo //B runasNSPostgres.vbs ""%LG_PATH%\initdb.exe" --locale=C --encoding=UTF-8  -U %DBADMIN% -D "%DBDATA%""

When this command is executed, another command prompt screen opens up which starts the initdb processing. I do not want the new cmd prompt screen to show up. I want the initdb.exe to run in the background.

1

1 Answers

0
votes

If you hide the command prompt window, you won't be able to use SendKeys to send it your password keystrokes.

You can use another method, though. Try using the ECHO command and pipe its output to RunAs.

oShell.Run "echo " & PASSWORD & " | runas /noprofile ...", 0

Use a 0 as the 2nd parameter to prevent the window from appearing.