Display cmd data into HTA and the command prompt window

85 Views Asked by At

I am stuck on an issue trying to get away from the cmd popup and have all the data from the command to appear in span tag. I know just enought to get the command to work but not sure how to make it output into HTA. If somebody can please help me fill in the blanks I would be greatful. Again I am not the best at this coding what so ever.

<!doctype html>
<html>
<head>
<title>My HTA</title>
<meta http-equiv="X-UA-Compatible" content="IE=9">

<style>
body {  font-family: 'Segoe UI'; font-size: 10pt; }
</style>


<HTA:APPLICATION
ID="MyHTA"
INNERBORDER="no"
CONTEXTMENU="no"
/>

<script>
window.onload = function sizeIt()
{
var sw = (screen.width-500)/2;
var sh = (screen.height-300)/2;
window.moveTo(sw,sh);window.resizeTo(678,590)
}

</script>

<script language="VBScript">

Sub displayinfo()

Dim infox
infox = (txt_info.value)
Set oShell = CreateObject ("WScript.Shell") 
oShell.run "cmd.exe /k" & infox
Set oShell = Nothing
dataarea.innerhtml = "<font color='red'><b>URL: </b></font>" & infox
End Sub
</script>



</head>

<body bgcolor="gray">

<!--{{InsertControlsHere}} - Do not remove this line-->

<div align="center">
<font color="blue"><b>UR</b>L</font> <b>:</b> <input type="text"  size="60" name="txt_info">
<br><br><br>
<input type="button" value="Submit" onClick="displayinfo()">
<br><br><br>
<span id="dataarea" style="vertical-align: middle;"><center>Information comes out here</center></span>
</div>

</body>
</html>

I read other post that was close to it but I cannot figure it out from what I am trying to do.

1

There are 1 best solutions below

5
LesFerch On

Creating a GUI to take a console command and then display the result is reinventing the wheel. You're better off using the console, especially if you want to see the results as they're generated (i.e. for long running commands). See this discussion for details:

HTA as a Command Line GUI

However, if it's for a special situation (i.e. not a full console replacement) and you're okay with getting the output in one splat, then you can use the Run method to hide the console window and direct the output to a temp file to read back into a variable in the HTA.

Here's an example, based on the code in the question.

VBScript Version

<!doctype html>
<html>
<head>
<title>My HTA</title>
<meta http-equiv="X-UA-Compatible" content="IE=9">
<HTA:APPLICATION
ID=MyHTA
ICON=cmd.exe
CONTEXTMENU=no
>
<script language="VBScript">

Set oWSH = CreateObject ("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

width = 700
height = 800
Window.ResizeTo width, height
Window.MoveTo (screen.availWidth - width)/2, (screen.availHeight - height)/2

Sub displayinfo
  outfile = oWSH.ExpandEnvironmentStrings("%Temp%") & "\cmdout.txt"
  oWSH.Run "cmd.exe /c " & txt_info.value & ">" & outfile,0,True
  dataarea.innerText = oFSO.OpenTextFile(outfile).ReadAll
End Sub

</script>
<style>
  body {font-family: 'Segoe UI'; font-size: 10pt; background-color: #202020; color:GhostWhite}
  .output {color: #CDECA8; vertical-align: middle}
  #cmdarea {text-align: center}
  #dataarea {text-align: center}
</style>
</head>
<body>
  <div id=cmdarea>
    Enter command: <input type="text" size="60" name="txt_info">
    <br><br><br>
    <input type="button" value="Submit" onClick="displayinfo()">
    <br><br><br>
    <span class="output" id="dataarea">Information comes out here</span>
  </div>
</body>
</html>

JScript Version

<!doctype html>
<html>
<head>
<title>My HTA</title>
<meta http-equiv="X-UA-Compatible" content="IE=9">
<HTA:APPLICATION
ID=MyHTA
ICON=cmd.exe
CONTEXTMENU=no
>
<script language="JScript">

var oWSH = new ActiveXObject("WScript.Shell");
var oFSO = new ActiveXObject("Scripting.FileSystemObject");

var width = 700;
var height = 800;
window.resizeTo(width, height);
window.moveTo((screen.availWidth - width) / 2, (screen.availHeight - height) / 2);

function displayinfo() {
  var outfile = oWSH.ExpandEnvironmentStrings("%Temp%") + "\\cmdout.txt";
  oWSH.Run("cmd.exe /c " + txt_info.value + ">" + outfile, 0, true);
  var file = oFSO.OpenTextFile(outfile);
  dataarea.innerText = file.ReadAll();
  file.Close();
}

</script>
<style>
  body {font-family: 'Segoe UI'; font-size: 10pt; background-color: #202020; color:GhostWhite}
  .output {color: #CDECA8; vertical-align: middle}
  #cmdarea {text-align: center}
  #dataarea {text-align: center}
</style>
</head>
<body>
  <div id=cmdarea>
    Enter command: <input type="text" size="60" name="txt_info">
    <br><br><br>
    <input type="button" value="Submit" onClick="displayinfo()">
    <br><br><br>
    <span class="output" id="dataarea">Information comes out here</span>
  </div>
</body>
</html>

Note that JScript is much more case-sensitive than VBScript. For example "innerText" can be any case in VBScript, but must be "innerText" in JScript.

Also note that in VBSCript, the file gets closed immediately after the readall without additional code, but in JScript, the file will remain open until the HTA is closed, so we should have an explicit close in JScript.