Get Output of a PowerShell Script in a HTA

≡放荡痞女 提交于 2019-12-28 15:56:12

问题


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.


回答1:


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>



回答2:


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>



回答3:


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.



来源:https://stackoverflow.com/questions/35198533/get-output-of-a-powershell-script-in-a-hta

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!