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.
This works for me:
test.ps1:
test.hta:
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: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.
This is another example showing you how to get the output result in a textarea while you execute a powhershell file with a HTA !