问题
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()
回答1:
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()
来源:https://stackoverflow.com/questions/33395369/real-time-data-with-powershell-gui