Search code examples
javascripthtmlbatch-filecmdhta

Run CMD command from hta batch file?


I have an hta script (batch file), which shows me several buttons and when I click these I want a command to be executed in my cmd. For example, when I press the "IP address" button, the command "ipconfig" should be entered in the cmd and executed. What can I do? Or is that even possible or are there other options for executing a command via this hta script?

Kind regards, jcjms

Here is my code:

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

Solution

  • In short, you need:

    onclick="callShellApplication('ipconfig')"
    

    A working example might look something like this:

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