获取HTA中PowerShell脚本的输出

获取HTA中PowerShell脚本的输出,powershell,hta,Powershell,Hta,我正在尝试从HTML应用程序[HTA]调用powershell脚本,如下所示: Set WshShell = CreateObject("WScript.Shell") Set retVal = WshShell.Exec("powershell.exe C:\PS_Scripts\test.ps1") 其中test.ps1只是返回进程计数 return (Get-Process).Count 我希望获取此powershell脚本的输出,然后将其存储在局部变量中或显示在HTA上。如何做到

我正在尝试从HTML应用程序[HTA]调用powershell脚本,如下所示:

Set WshShell = CreateObject("WScript.Shell")

Set retVal = WshShell.Exec("powershell.exe  C:\PS_Scripts\test.ps1")
其中test.ps1只是返回进程计数

return (Get-Process).Count
我希望获取此powershell脚本的输出,然后将其存储在局部变量中或显示在HTA上。如何做到这一点

我尝试使用:

retVal.StdIn.Close()

result = retVal.StdOut.ReadAll()


alert(result)
但打印的结果值为空

请帮助我如何做到这一点。

这对我来说很有用:

测试1.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试验

这是另一个示例,演示如何在使用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>

Hackoo使用HTA执行powershell文件
选项显式
子运行\u PS\u脚本()
ExampleOutput.value=“”
btnClick.disabled=True
document.body.style.cursor=“等待”
btnClick.style.cursor=“等待”
Dim WshShell、命令、PSFile、返回、fso、文件、文本、临时
设置WshShell=CreateObject(“WScript.Shell”)
Temp=WshShell.expandEnvironmentString(“%Temp%”)
Command=“cmd/c echo Get WmiObject Win32_Process^|选择ProcessID、ProcessName、Handle、commandline、ExecutablePath^|输出文件%temp%\output.txt-编码ascii>%temp%\Process.ps1”
PSFile=WshShell.Run(命令,0,True)
return=WshShell.Run(“powershell.exe-ExecutionPolicy Unrestricted-File%temp%\process.ps1”,0,true)
设置fso=CreateObject(“Scripting.FileSystemObject”)
Set file=fso.OpenTextFile(Temp&“\output.txt”,1)
text=file.ReadAll
ExampleOutput.Value=文本
文件,关闭
document.body.style.cursor=“默认”
btnClick.style.cursor=“默认”
btnClick.disabled=False
端接头

您可以使用WScript.Shell的
Exec
方法来避免中间文件。不幸的是,它在运行时会打开一个新窗口,但是代码要干净得多,并且允许您访问StdOut和StdErr流。将其粘贴到.HTA文件中(如果需要,带有标题和正文)以测试:


var-proc//全球范围
函数execWithStatus(cmdLine){//无法使用Exec运行。无法使用run捕获StdOut/StdErr。
proc=newActiveXObject(“WScript.Shell”).Exec(cmdLine);
setTimeout(“writeOutLine()”,100);//暂停100毫秒以允许标准输出流传输某些数据
proc.StdIn.Close();//必须关闭输入才能完成powershell命令
}
函数writeOutLine(){
如果(!proc.StdErr.AtEndOfStream){txtreults.value+=“错误:”+proc.StdErr.ReadAll()+“\n”;}
如果(!proc.StdOut.AtEndOfStream){txtreults.value+=proc.StdOut.ReadLine()+“\n”;writeOutLine();}
}
powershell.exe-非交互式-命令返回(获取进程)。计数
跑


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

你能给代码添加一些格式以使其更易于阅读吗?可能是@Marc的重复我也尝试了上述解决方案,将输出写入文件。但文件内容再次为空。powershell在powershell控制台上正确打印输出(进程数)。HTML应用程序无法检索此信息。我认为您可以使用
start/b
自动最小化打开的终端。
<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>