运行 来自 hta 批处理文件的 CMD 命令?

Run CMD command from hta batch file?

我有一个 hta 脚本(批处理文件),它显示了几个按钮,当我单击这些按钮时,我希望在我的 cmd 中执行一个命令。例如,当我按下“IP地址”按钮时,应该在cmd中输入命令“ipconfig”并执行。我能做什么?或者这是否可能,或者是否有其他选项可以通过此 hta 脚本执行命令?

亲切的问候, jcjms

这是我的代码:

<!-- :: Batch section
@echo off
setlocal

echo Select an option:
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
echo End of HTA window, reply: "%HTAreply%"
goto :EOF
-->


<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >
<body bgcolor="black">
<TITLE>HTA Buttons</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(374,400);


function sleepFor( sleepDuration ){
    var now = new Date().getTime();
    while(new Date().getTime() < now + sleepDuration){ /* do nothing */ } 
}

function callShellApplication(command){
    var args=document.getElementById('args').value;
    var r = new ActiveXObject("WScript.Shell");
    var res=r.Exec(command +" "+args);    
    var out="";
    while (res.Status == 0)
    {
         sleepFor(100);
    }
    while (!res.StdOut.AtEndOfStream){
        out=out+"\r\n"+res.StdOut.ReadLine();
    }

    var StS=new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
    StS.Write(out);     
    //new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(res.StdOut.ReadLine());   
    window.close();
}

function closeHTA(reply){
   var fso = new ActiveXObject("Scripting.FileSystemObject");
   fso.GetStandardStream(1).WriteLine(reply);
   window.close();
}


</SCRIPT>
</HEAD>
<BODY>
   Aruments:<textarea name="args" style="display:none; cols="40" rows="1"></textarea>
   <h1 style="color:white;font-size:40px;">Commands</h1>   
   <hr>
   <button style="background-color:black; border-color:white; color:white" onclick='[what to do here? I want to shutdown other PCs in my local network over the window that opens in cmd (shutdown /iCODELOCK)]'>PC shutdown</button>
   <button style="background-color:black; border-color:white; color:white" onclick='[what to do here? I want to get the PCs IP in cmd (ipconfig)]'>IP-Adress</button>
   <button style="background-color:black; border-color:white; color:white" onclick="closeHTA(1);">close</button>
</BODY>
</HTML>

注意那些方法应该是贬值的它是一个 html 布局文件,具有宽松的安全性,因此你必须小心你如何制作你的 cmd,javascript,vb 脚本或 PowerShell 命令。

那个混合格式最简单的方法是用vb方法生成和运行按钮混合匹配没有问题

像这样使用更简单的主体结构,但要确保您的 JavaScript 能够传递参数

<HTML>
  <HEAD>
    <TITLE>HTA Example</TITLE>
  </HEAD>
  <BODY>
    <FORM>
      <INPUT TYPE="Run" NAME="Button" VALUE="Click Me">

      <SCRIPT FOR="Button" EVENT="onClick" LANGUAGE="VBScript">
        Set WshShell = CreateObject("WScript.Shell")
        WshShell.Run "cmd.exe /k echo off & echo/ & echo this could run IPconfig &IPconfig /? |more +38"
      </SCRIPT>

    </FORM>
  </BODY>
</HTML>

有关安全性和增强功能(如 SVG 图形)的更多信息,您需要查看 https://docs.microsoft.com/en-us/previous-versions/ms536496(v=vs.85)

你切换了问题,添加了应该在 OP 中的代码,所以我仍然建议最简单的方法是将你的调用调整为简单的 vbscript

在你的脑海中添加测试

      <SCRIPT FOR="shutdown" EVENT="onClick" LANGUAGE="VBScript">
        Set WshShell = CreateObject("WScript.Shell")
        WshShell.Run "cmd.exe /k echo change /k to /c and remove this comment including echo's SHUTDOWN /iCODELOCK"
      </SCRIPT>

      <SCRIPT FOR="ipconfig" EVENT="onClick" LANGUAGE="VBScript">
        Set WshShell = CreateObject("WScript.Shell")
        WshShell.Run "cmd.exe /k IPconfig"
      </SCRIPT>

将您的按钮更改为

   <button style="background-color:black; border-color:white; color:white" name="shutdown">PC shutdown</button>
   <button style="background-color:black; border-color:white; color:white" name="ipconfig">IP-Adress</button>
   <button style="background-color:black; border-color:white; color:white" onclick="closeHTA(1);">close</button>

简而言之,您需要:

onclick="callShellApplication('ipconfig')"

一个工作示例可能如下所示:

<html>
<head>
    <HTA:APPLICATION SCROLL="yes" SYSMENU="no" />
    <title>HTA Buttons</title>
    <script language="JScript">
    //window.resizeTo(374,400);

    function callShellApplication(command){
        var shell = new ActiveXObject("WScript.Shell"),
        exec = shell.Exec(command);
        output.innerHTML = exec.StdOut.ReadAll().replace(/\r\n/g, '<br>');
    }
    </script>
    <style>
        *{color:white}
        button{background-color:black; border-color:white}
    </style>
</head>
<body bgcolor="black">
    <h1 style="font-size:40px;">Commands</h1>   
    <hr>
    <button onclick="callShellApplication('shutdown /s')">PC shutdown</button>
    <button onclick="callShellApplication('ipconfig')">IP-Adress</button>
    <button onclick="window.close()">close</button>
    <hr>
    <pre id="output"></pre>
</body>
</html>