Get PowerShell Script output in HTA

I am trying to call a powershell script from an HTML [HTA] application like:

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

If test.ps1 just has a counter of processes returning

 return (Get-Process).Count 

I want to get the result of this powershell script, and then save it in a local variable or map to HTA. How can I do that?

I tried using:

 retVal.StdIn.Close() result = retVal.StdOut.ReadAll() alert(result) 

But the value of the printed result is null.

Please help me how to achieve this.

+5
source share
2 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> 
+4
source

This is another example showing you how to get the result in a text box while you execute a command-line file using 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> 
+3
source

Source: https://habr.com/ru/post/1242284/


All Articles