Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run command line in the background using vbs

Status
Not open for further replies.

pankajarora

Programmer
May 21, 2014
1
0
0
US
Hi,

I am new to VBScripting and so need some help. Following is the (runas.vbs) script that I am using to use the RunAs command in command prompt:

Option explicit
Dim oShell, k, strarg
Const PASSWORD = "Password123~"
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 call this vbscript from a batch file. When this vbs is executed while running the batch file, it opens another command prompt on the top of the original one. I want to know if there is a way to hide the new command prompt window which gets opened by this vs so that it can run in the background and not show up.

Thanks
 
the window that opens is the "RunsAs" command called by oShell.Run. According to RunAs /?, there is no "hide" option. It appears child processes are hidden but you can try to run the oShell.Run in a hidden state.

oShell.run ("RunAs /noprofile /user:%computername%\postgres " & Chr(34) & "cmd /c\" & Chr(34) & WScript.Arguments(0) & "\" & Chr(34) & Chr(34), 0[\b])

Mind you, though, that this won't help with your problem because oShell.SendKeys command needs window focus in order to send keystrokes.

NOTE: oShell.SendKeys (PASSWORD) should work just fine. No for loop needed.

Example: This script acts unintentionally. When the red text is removed from the objShell.Run command, the script creates a text document called notepad.txt with the content "text".
Code:
set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "Notepad.exe"[red][b], 0[/b][/red]
wscript.sleep 1000
objShell.SendKeys "test{f6}"
wscript.sleep 1000
objShell.sendkeys "notepad.txt{enter}"

-Geates

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top