Avatar of Lico_w
Lico_w
 asked on

How to grab a hidden or minimised DOS prompt and then use the SendKeys method

I have a simple script which runs a bat file, which opens up a telnet session to a box and port, dumps the output to a logfile and then closes the connection, discussed in the following article: 27238825.However the only way to close it due to various issues is to grab the DOS window and do a SendKey of CTRL + ] and then a sendkey of quit, then enter.

This works fine when the telnet session is opened normally however there is a flaw. If the user clicks onto a separate window whilst the command is running, the sendkeys go to that window and dont close the telnet session. I would ideally like the DOS screen to be hidden or minimised so users don't see it. To do that I would need to grab the window manually I guess and then do the sendkeys thing. My script is below
newLog = 1

for i = 1 to 10
	monitorSS(newLog)
	newLog = newLog + 1
next

function monitorSS(logNo)
	sCmd = "monitorSS.bat "
	Set oShell = CreateObject("WSCript.shell") 

	oShell.Run sCmd & logNo
	WScript.Sleep 500
	oShell.AppActivate "C:\Windows\system32\cmd.exe"
	oShell.SendKeys("^]")
	WScript.Sleep 500
	oShell.AppActivate "C:\Windows\system32\cmd.exe"
	oShell.SendKeys("quit{ENTER}")
	WScript.Sleep 500

	set oShell = nothing
end function

Open in new window

VB Script

Avatar of undefined
Last Comment
jorgedeoliveiraborges

8/22/2022 - Mon
jorgedeoliveiraborges

Did you try to use taskkill COMMAND?

See: taskkill /?
ASKER CERTIFIED SOLUTION
jorgedeoliveiraborges

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes