Get Output of a PowerShell Script in a HTA

前端 未结 3 1490
无人及你
无人及你 2020-12-10 07:25

I am trying to call a powershell script from HTML Application [HTA] as :

Set WshShell = CreateObject(\"WScript.Shell\")

Set retVal = WshShell.Exec(\"powersh         


        
相关标签:
3条回答
  • 2020-12-10 07:44

    You can use the Exec method of WScript.Shell to avoid intermediate files. Unfortunately it opens a new window when it runs, but the code is much cleaner and gives you access to the StdOut and StdErr streams. Paste this into an .HTA file (with header and body if desired) to test:

    <script language="Javascript">
    var proc; //global scope
    function execWithStatus(cmdLine){//Can't run minimized with Exec. Can't capture StdOut/StdErr with Run. 
        proc = new ActiveXObject("WScript.Shell").Exec(cmdLine);
        setTimeout("writeOutLine()",100);//pause for 100 ms to allow StdOut to stream some data 
        proc.StdIn.Close();//must close input to complete a powershell command    
    }
    function writeOutLine(){
        if(!proc.StdErr.AtEndOfStream) {txtResults.value += "ERROR: " + proc.StdErr.ReadAll() + "\n";}
        if(!proc.StdOut.AtEndOfStream) {txtResults.value += proc.StdOut.ReadLine() + "\n";writeOutLine();} 
    }
    </script>
    <textarea id=txtCmd style="width:90%" rows=1>
    powershell.exe -noninteractive -command return (Get-Process).Count</textarea> 
    <button onclick="execWithStatus(txtCmd.value)">Run</button>
    <br><textarea id=txtResults style="width:100%" rows=20></textarea> 
    

    Part of your problem may have been that Exec isn't blocking waiting for StdOut to start filling up. Adding the timer corrected that issue for me.

    0 讨论(0)
  • 2020-12-10 07:49

    This is another example showing you how to get the output result in a textarea while you execute a powhershell file with a HTA !

    <html>
    <head>
    <title>Execution a powershell file with HTA by Hackoo</title>
    <HTA:APPLICATION 
         APPLICATIONNAME="Execution a powershell file with HTA by Hackoo"
         SCROLL="yes"
         SINGLEINSTANCE="yes"
         WINDOWSTATE="maximize"
         ICON="Winver.exe"
         SCROLL="no"
    />
    <script language="VBScript">
    Option Explicit
    Sub Run_PS_Script()
        ExampleOutput.value = ""
        btnClick.disabled = True
        document.body.style.cursor = "wait"
        btnClick.style.cursor = "wait"
        Dim WshShell,Command,PSFile,return,fso,file,text,Temp
        Set WshShell = CreateObject("WScript.Shell")
        Temp = WshShell.ExpandEnvironmentStrings("%Temp%")
        Command = "cmd /c echo Get-WmiObject Win32_Process ^| select ProcessID,ProcessName,Handle,commandline,ExecutablePath ^| Out-File %temp%\output.txt -Encoding ascii > %temp%\process.ps1"
        PSFile = WshShell.Run(Command,0,True)
        return = WshShell.Run("powershell.exe -ExecutionPolicy Unrestricted -File %temp%\process.ps1", 0, true)
        Set fso  = CreateObject("Scripting.FileSystemObject")
        Set file = fso.OpenTextFile(Temp &"\output.txt", 1)
        text = file.ReadAll 
        ExampleOutput.Value=text
        file.Close
        document.body.style.cursor = "default"
        btnClick.style.cursor = "default"
        btnClick.disabled = False   
    End Sub
    </script>
    </head>
    <body bgcolor="123456">
    <textarea id="ExampleOutput" style="width:100%" rows="37"></textarea>
    <br>
    <center><input type="button" name="btnClick" value="Run powershell script file " onclick="Run_PS_Script()"></center> 
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-10 07:59

    This works for me:

    test.ps1:

    (Get-Process).Count | Out-File c:\temp\output.txt -Encoding ascii
    

    test.hta:

    <head>
    <title>HTA Test</title>
    <HTA:APPLICATION 
         APPLICATIONNAME="HTA Test"
         SCROLL="yes"
         SINGLEINSTANCE="yes"
         WINDOWSTATE="maximize"
    </head>
    <script language="VBScript">
        Sub TestSub
    
            Set WshShell = CreateObject("WScript.Shell")
            return = WshShell.Run("powershell.exe -ExecutionPolicy Unrestricted -File test.ps1", 0, true)
            Set fso  = CreateObject("Scripting.FileSystemObject")
            Set file = fso.OpenTextFile("c:\temp\output.txt", 1)
            text = file.ReadAll     
            alert(text)     
            file.Close      
        End Sub
    </script>
    <body>
        <input type="button" value="Run Script" name="run_button"  onClick="TestSub"><p> 
    </body>
    
    0 讨论(0)
提交回复
热议问题