Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real Time Data With Powershell GUI

I'm struggling here.

Using Powershell and GUI, how to automatically refresh data on a form?

Example with the script below, how to automatically update the label with the number of process executed by my computer?

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()
like image 667
Nick32342 Avatar asked Sep 18 '25 06:09

Nick32342


1 Answers

You need to add a timer to the form to do the updating for you. Then add the code you need to gather the process count to the .Add_Tick, as shown below:

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()
like image 190
Eric Walker Avatar answered Sep 19 '25 18:09

Eric Walker