0

I need to simulate user input in some sentences using cmd (command prompt) in windows 7+, I´ve searched here and found a good partial solution:

[PARTIAL SOLUTION] how to userinput without typing to a batch file

It works in most cases, but for some reason, fails in others. It works well, for example, with:

cmd /c echo y^> "%temp%\answer.tmp" ^& (chkdsk C: /F /R /X ^< "%temp%\answer.tmp") ^& del "%temp%\answer.tmp"

It creates a file named "answer.tmp" with two lines, one with the response ("y" in this case) and then pases it to"chkdsk C: /F /R /X" command and last, deletes it.

But, for some reason, it fails with this:

cmd /c echo y^> "%temp%\answer.tmp" ^& (dism /online /disable-feature /featurename:"TabletPCOC" ^< "%temp%\answer.tmp") ^& del "%temp%\answer.tmp"

The only thing changed is the command executed, this time "dism /online /disable-feature /featurename:'TabletPCOC'".

Do you have any Ideas?

Community
  • 1
  • 1
  • What happens when you try this interactively in a console window? (Run `echo y > "%temp%\answer.tmp"` first, then run `dism /online /disable-feature /featurename:"TabletPCOC" < "%temp%\answer.tmp"`.) – mklement0 Nov 05 '15 at 22:51
  • this trick works only, if the program ("target") takes it's input from STDIN. Some programs prefer to get their input directly from the keyboard (not sure, if `dism` is one of them, but keep that in mind). – Stephan Nov 12 '15 at 16:44

2 Answers2

0

This is a small (Not really...) PowerShell program that can do what you want:

add-type -AssemblyName microsoft.VisualBasic

add-type -AssemblyName System.Windows.Forms

start cmd.exe 

start-sleep -Milliseconds 500

[Microsoft.VisualBasic.Interaction]::AppActivate("cmd.exe")

[System.Windows.Forms.SendKeys]::SendWait("bluh")

Note that start Notepad.exe is optional but this doesn't work if its not open. The start-sleep command will give a little timer, which is useful because it give the computer a chance to load Notepad.exe .

So to run this in cmd:

powershell.exe add-type -AssemblyName microsoft.VisualBasic;add-type -AssemblyName System.Windows.Forms;start Notepad.exe;start-sleep -Milliseconds 500;[Microsoft.VisualBasic.Interaction]::AppActivate("Notepad");[System.Windows.Forms.SendKeys]::SendWait("bluh")

Roke
  • 300
  • 1
  • 6
  • 27
  • Since you want to do `dism /online /disable-feature /featurename:"TabletPCOC` you would put that instead of bluh. REMEBER QUOTES. – Roke Nov 06 '15 at 01:39
0

Thanks @RookieTEC9 (I can't comment your response yet) but it only works if only one cmd.exe instance is running...

Thanks @mklement0, when I execute it directly, it correctly adds the "y" response to file, but the command still gets stuck at user response prompt.

edgerch
  • 161
  • 5