Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Output of a PowerShell Script in a HTA

Tags:

powershell

hta

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

Set WshShell = CreateObject("WScript.Shell")

Set retVal = WshShell.Exec("powershell.exe  C:\PS_Scripts\test.ps1")

Where the test.ps1 just has the process count returning

return (Get-Process).Count

I want to get the output of this powershell script and then store it in a local variable or display on HTA. How can this be done ?

I tried using :

retVal.StdIn.Close()

result = retVal.StdOut.ReadAll()


alert(result)

But the printed result value is null.

Please help me how to achieve this.

like image 566
shanky Avatar asked Feb 04 '16 10:02

shanky


3 Answers

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>
like image 111
Marc Avatar answered Oct 18 '22 21:10

Marc


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>
like image 21
Hackoo Avatar answered Oct 18 '22 22:10

Hackoo


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.

like image 20
Rich Moss Avatar answered Oct 18 '22 23:10

Rich Moss