How to auto refresh instead using a button

Hi i’ve got a ps1 that monitor a MDT server and on that ps1 with a xaml i’m using a button to refresh the info…my button is doing

$Refresh_btn.Add_Click({	
	$Properties_Row_List.Clear()			
	Populate_DataGrid_Monitoring
	$wshell = New-Object -ComObject Wscript.Shell
	$wshell.Popup("Rafraichissement des données effectué.",0,"Attention",0x0)
})

Instead of using that button I would like to “Auto” refresh every 1-2 minutes… could you help me on that?

Thanks

If you want something to occur, non-stop, every two minutes, you could do something like:

Do
{
  $Properties_Row_List.Clear()			
  Populate_DataGrid_Monitoring
  $wshell = New-Object -ComObject Wscript.Shell
  $wshell.Popup("Rafraichissement des données effectué.",0,"Attention",0x0)
  Start-Sleep -Seconds 120
}
While($True)

Thanks…But I realized that when the Do/While is running it keep the “focus” so no other execution is possible. So only @ every 120s, other action is able to run :confused:

By the fact that you’re trying to auto run code that is currently manually activated by a button, you’re running a gui application. Not sure if the below is relevant/will help (It’s from a “Tip of the Day” e-mail from powershell.com), but it’s about having the powershell title bar updated via a background thread so that you’re able to still interact with the powershell console while the background thread keeps updating the title bar. Maybe someone else has a better idea/more details.

Adding Live Clock to PowerShell Title Bar (Part 1)
All Versions
To continuously update the PowerShell title bar, and for example display the current date and time, you need a background thread that takes care of this. Without a background thread, your PowerShell would be busy all the time updating the title bar, thus unusable…
Here is a simple piece of code you can run to display a live clock in your title bar:
$code =
{
# submit the host process RawUI interface and the execution context
param($RawUi)

do
{
    # compose the time and date display
    $time = Get-Date -Format 'HH:mm:ss dddd MMMM d'
    # compose the title bar text
    $title = "Current Time: $time"
    # output the information to the title bar of the host process
    $RawUI.WindowTitle = $title
    # wait a half second
    Start-Sleep -Milliseconds 500
} while ($true)

}
$ps = [PowerShell]::Create()
$null = $ps.AddScript($code).AddArgument($host.UI.RawUI)
$handle = $ps.BeginInvoke()
The crucial thing here is to pass over the $host.UI.RawUI object from your foreground PowerShell to your background thread code. Only then can your background thread access the title bar owned by your foreground PowerShell.

Thanks i’ve changed a bit the code and went with a timer with a stop button…

 
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 60000 # milliseconds 1min
$timer.add_tick({UpdateUi})
 
Function UpdateUi()
{
        $wshell = New-Object -ComObject Wscript.Shell
	$wshell.Popup("Start refresh.",0,"Attention",0x0)
}
 
Function StartTimer()
{
    $timer.start()
}
 
Function StopTimer()
{
    $timer.Stop()
	$wshell = New-Object -ComObject Wscript.Shell
	$wshell.Popup("Stop refresh.",0,"Attention",0x0)
 
}

Working great so far, thanks