在 HTA 中获取 PowerShell 脚本的输出

Get Output of a PowerShell Script in a HTA

我正在尝试从 HTML 应用程序 [HTA] 中调用 powershell 脚本作为 :

Set WshShell = CreateObject("WScript.Shell")

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

测试的地方。ps1 只是有进程计数返回

return (Get-Process).Count

我想获取此 powershell 脚本的输出,然后将其存储在局部变量中或显示在 HTA 上。如何做到这一点?

我尝试使用:

retVal.StdIn.Close()

result = retVal.StdOut.ReadAll()


alert(result)

但是打印出来的结果值为空

请帮我看看如何实现。

这对我有用:

测试。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>

这是另一个示例,向您展示如何在使用 HTA 执行 powershell 文件时在文本区域中获取输出结果!

<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>

可以使用WScript.Shell的Exec方法来避免中间文件。不幸的是,它在运行时会打开一个新的 window,但是代码更简洁,并且可以让您访问 StdOut 和 StdErr 流。将其粘贴到 .HTA 文件中(如果需要,使用 header 和 body)以测试:

<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> 

您的部分问题可能是 Exec 没有阻止等待 StdOut 开始填充。添加计时器为我纠正了这个问题。