使用Powershell GUI实时数据

时间:2015-10-28 15:38:55

标签: powershell powershell-v2.0 powershell-v3.0 powershell-v4.0 powershell-ise

我在这里挣扎。

使用Powershell和GUI,如何自动刷新表单上的数据?

使用下面的脚本示例,如何使用我的计算机执行的进程数自动更新标签?

Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Sample Form"
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Number of process executed on my computer"
$Label.AutoSize = $True
$Form.Controls.Add($Label)
$Form.ShowDialog()

1 个答案:

答案 0 :(得分:1)

您需要在表单中添加一个计时器才能为您进行更新。然后将收集进程数所需的代码添加到.Add_Tick中,如下所示:

function UpdateProcCount($Label)
{
    $Label.Text = "Number of processes running on my computer: " + (Get-Process | measure).Count
}

$Form = New-Object System.Windows.Forms.Form
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
$timer.Add_Tick({UpdateProcCount $Label})
$timer.Enabled = $True
$Form.Text = "Sample Form"
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Number of process executed on my computer"
$Label.AutoSize = $True
$Form.Controls.Add($Label)
$Form.ShowDialog()