2012-04-02 45 views
1

我正试图将数据从文本文件写入到HTA中。从文本文件写入到<span>的HTA

我运行PowerShell脚本的HTA内,使用VBScript为输入按钮

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

我将使用每个循环约60台服务器

然后我希望能输出写入的HTA中,有点像VB中的一个流光或堆叠的字符串VBScript中,是这样的:

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

,但它似乎应该有一个SIM卡更简单的方法来做到这一点。

+0

我真的不明白你的问题。您无法从poweshell生成行?为什么在PowerShell中为你使用HTA而不是Windows窗体? – JPBlanc 2012-04-03 07:20:46

+0

大多数情况下,我一直在寻找一个可选的图形用户界面来完整的在VB上,这可能比它的价值更痛苦,我已经玩了一些教程HTA的VBScript,并喜欢他们提供的灵活性和简单的学习曲线,甚至埃德威尔逊MS的powershell posterboy建议将它们用于powershell脚本http://blogs.technet.com/b/heyscriptingguy/archive/2009/08/31/hey-scripting-guy-can-i-create-a-gui-for-a-windows -powershell-script.aspx,如果有更好的方法随时指向我在那个方向 – 2012-04-04 06:54:26

回答

0

好的这是我使用的方式。

从理论的角度来看,它构建了一个Windows窗体接口,然后将PowerSell代码放在事件后面。

在技术上看两种解决方案:

1)使用Visual Studio免费版建立在C#接口,然后转换工具来创建关联的PowerShell源(french article here

2)你可以自由下载(你只需要注册)Sapiens PrimalFormsCE.exe(社区版)

PrimalFormsCE download

这个工具允许您创建一个表单和然后生成Powershell相关代码。

PrimalFormsCE image

您还可以在这里建立从崩溃的形式是示例代码和平:

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

看起来非常像VB6,我会检查出来的谢谢,虽然我仍然认为有可能做我在问什么,在至少根据MS来看,就是没有那么好的例子 – 2012-04-05 08:17:05

1

我觉得这个最小HTA将解决你的问题。它运行命令行并读取输出流,每1/10秒一行,然后将结果推送到textarea。您可能想要更改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 textarea的内容是你的命令行,并给它一试。祝你好运!