【问题标题】:HTA writing to a <span> from a text fileHTA 从文本文件写入 <span>
【发布时间】:2012-05-10 08:57:16
【问题描述】:

我正在尝试将数据从文本文件写入 HTA 中。

我在 HTA 中运行一个 powershell 脚本,使用 VBscript 作为输入按钮

Get-TSSession -computername ismeta | where { $_.username -eq 'amis5235'} | format-table windowstationname,username,state,sessionid | out-file C:\windows\temp\PSTerminalServices.txt

我将为大约 60 台服务器使用 for each 循环

然后我希望将输出写入 HTA 中,有点像 VB 中的流媒体或堆叠 VBscript 的字符串,类似于:

strHTML = strHTML & "Running Process = " & objProcess.Name & " PID = " & objProcess.ProcessID & " Description = " & objProcess.Description & "<br>"

但似乎应该有更简单的方法来做到这一点。

【问题讨论】:

  • 我不太明白你的问题。您无法从 poweshell 生成 行吗?为什么你在 PowerShell 中为你的 UI 使用 HTA 而不是 Windows 窗体?
  • 我主要是在寻找一个替代的 GUI 来替代完整的 VB,这可能比它的价值更痛苦,我玩过一些用于 VBScript 的教程 HTA,并且喜欢它们的灵活性和简单的学习曲线提供,甚至 Ed Wilson MS 的 powershell posterboy 也建议将它们用于 powershell 脚本 blogs.technet.com/b/heyscriptingguy/archive/2009/08/31/…,如果有更好的方法,请随时指出我的方向

标签: powershell hta


【解决方案1】:

我认为这个最小的 HTA 将解决您的问题。它运行命令行并读取输出流,每 1/10 秒一行,然后将结果推送到文本区域。您可能希望更改您的 Powershell 脚本以将进程详细信息返回到 STDOUT,但它可能会起作用。

<script language="Javascript">
var E, LineWriteTimerID
function execWithStatus(cmdLine){//Can't run minimized with Exec. Can't capture StdOut/StdErr with Run. 
    E = new ActiveXObject("WScript.Shell").Exec(cmdLine);
    LineWriteTimerID = window.setInterval("writeOutLine()",100);//pause for 100ms
    E.StdIn.Close();//must close input to complete a ps command    
}
function writeOutLine(){
    if(E.StdOut.AtEndOfStream) window.clearTimeout(LineWriteTimerID);
    if(!E.StdErr.AtEndOfStream) txtResults.value += "ERROR: " + E.StdErr.ReadAll() + "\n";
    if(!E.StdOut.AtEndOfStream) txtResults.value += E.StdOut.ReadLine() + "\n";
}
</script>
<textarea id=txtCmd style="width:90%" rows=1>
powershell.exe -noninteractive -command ls c:\windows\system32\drivers\etc\</textarea> 
<button onclick="execWithStatus(txtCmd.value)">Run</button>
<br><textarea id=txtResults style="width:100%" rows=20></textarea> 

将此代码保存为 .HTA 文件,将 txtCmd 文本区域的内容更改为您的命令行,然后试一试。祝你好运!

【讨论】:

    【解决方案2】:

    好的,这就是我的使用方式。

    从理论上讲,它包括构建与 Windows 窗体的界面,然后将 PowerSell 代码放在事件后面。

    从技术角度来看两种解决方案:

    1) 使用 Visual Studio 免费版在 C# 中构建界面,然后使用转换工具创建关联的 PowerShell 源 (french article here)

    2)您可以免费下载(只需注册)Sapiens PrimalFormsCE.exe(社区版)

    此工具允许您创建一个表单,然后生成 Powershell 关联代码。

    您也可以从崩溃中构建表单,这里是一个简单的示例代码:

    Add-Type -AssemblyName system.Windows.Forms
    
    # Create the form
    $form = New-Object Windows.Forms.Form
    $form.Text = "Test Saisie"
    $form.Size = New-Object System.Drawing.Size(250,154)
    
    # Create EntryFiel
    $TB_Saisie = New-Object System.Windows.Forms.TextBox
    $TB_Saisie.Location = New-Object System.Drawing.Point(50,31)
    $TB_Saisie.Size = New-Object System.Drawing.Size(150,32)
    
    # Create "Ok" Button
    $PB_Ok = New-Object System.Windows.Forms.Button
    $PB_Ok.Text = "Ok"
    $PB_Ok.Location = New-Object System.Drawing.Point(50,62)
    $PB_Ok.Size = New-Object System.Drawing.Size(50,32)
    $PB_Ok.DialogResult = [System.Windows.Forms.DialogResult]::OK
    
    # Create "Cancel" Button
    $PB_Cancel = New-Object System.Windows.Forms.Button
    $PB_Cancel.Text = "Cancel"
    $PB_Cancel.Location = New-Object System.Drawing.Point(150,62)
    $PB_Cancel.Size = New-Object System.Drawing.Size(50,32)
    $PB_Cancel.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    # Add controls to the form
    $form.Controls.Add($PB_Ok)
    $form.Controls.Add($PB_Cancel)
    $form.Controls.Add($TB_Saisie)
    
    # Message loop
    $Res = $form.ShowDialog()
    If ($Res -eq [System.Windows.Forms.DialogResult]::OK)
    {
      Write-Host ("Accepted : {0}" -f $TB_Saisie.Text)
    }
    else
    {
      Write-Host "Cancel"
    }
    

    【讨论】:

    • 看起来非常像 VB6,我会检查一下谢谢,虽然我仍然认为可以按照我的要求做,至少根据 MS 是这样,只是没有很好的例子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多