将cmd数据显示到HTA和命令提示符窗口中

问题描述 投票:0回答:1

我遇到了一个问题,试图摆脱 cmd 弹出窗口并将命令中的所有数据显示在 span 标记中。我知道足以让命令正常工作,但不知道如何将其输出到 HTA 中。如果有人可以帮助我填补空白,我将不胜感激。再说一遍,我在编码方面并不是最好的。

<!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>

我读了其他接近它的帖子,但我无法从我正在尝试做的事情中弄清楚它。

cmd vbscript jscript hta
1个回答
0
投票

创建一个 GUI 来接受控制台命令然后显示结果就像是在重新发明轮子。您最好使用控制台,特别是如果您想查看生成的结果(即长时间运行的命令)。有关详细信息,请参阅此讨论:

HTA 作为命令行 GUI

但是,如果是针对特殊情况(即不是完整的控制台替换)并且您可以一次获得输出,那么您可以使用

Run
方法隐藏控制台窗口并将输出定向到临时文件以读回 HTA 中的变量。

这是一个基于问题中的代码的示例。

VBScript 版本

<!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 版本

<!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>

请注意,JScript 比 VBScript 更区分大小写。例如,“innerText”在 VBScript 中可以是任何大小写,但在 JScript 中必须是“innerText”。

另请注意,在 VBSCript 中,文件在 readall 后立即关闭,无需额外代码,但在 JScript 中,文件将保持打开状态,直到 HTA 关闭,因此我们应该在 JScript 中显式关闭。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.