【问题标题】:Get Output of a PowerShell Script in a HTA在 HTA 中获取 PowerShell 脚本的输出
【发布时间】:2018-10-27 00:33:33
【问题描述】:

我正在尝试将 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)

但是打印出来的结果值为null。

请帮助我如何实现这一目标。

【问题讨论】:

  • 您可以在代码中添加一些格式以使其更易于阅读吗?
  • @Marc 我也尝试了上述解决方案,将输出写入文件。但是文件内容又是空的。 powershell 在 powershell 控制台上正确打印输出(进程数)。 HTML 应用程序无法检索此信息。

标签: powershell hta


【解决方案1】:

这对我有用:

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

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

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

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

      <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 开始填充。添加计时器为我解决了这个问题。

      【讨论】:

      • 我想你可以使用start /b来自动最小化打开的终端。
      猜你喜欢
      • 2017-01-23
      • 1970-01-01
      • 2018-06-09
      • 2013-05-06
      • 1970-01-01
      • 2020-10-09
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多