HTA writing to a <span> from a text file

余生颓废 提交于 2019-12-24 14:56:17

问题


I am trying to write data from a text file to a in an HTA.

I'm running a powershell script inside of the HTA, using VBscript for the input buttons

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

I'm going to be using a for each loop for about 60 servers

Then I was hoping to write the output to a within the HTA, kind of like a streamer in VB or stacking a string the VBscript, something like:

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

but it seems there should be a simpler way to do this.


回答1:


I think this minimal HTA will solve your problem. It runs a command line and reads the output stream, one line every 1/10 second, then pushes the results into a textarea. You may want to alter your Powershell script to return the process details to STDOUT, but it will probably work.

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

Save this code as an .HTA file, change the contents of the txtCmd textarea to be your command line, and give it a try. Good Luck!




回答2:


Ok Here is the way I use.

On the theorical point of view it consist in building an interface with Windows Forms and then put PowerSell code behind event.

On technical point of view two solutions :

1) Use visual studio free edition to build interface in C# and then a conversion tool to create the associate PowerShell source (french article here)

2) you can download freely (you just need to register) Sapiens PrimalFormsCE.exe (Community Edition)

This tool allow you create a form and then to generate Powershell associete code.

You can also build forms from crash here is a peace of sample code :

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


来源:https://stackoverflow.com/questions/9984700/hta-writing-to-a-span-from-a-text-file

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