powershell timer

¿Cómo obtener un temporizador en un cuadro de mensaje de Powershell?



timer (2)

No veo una forma de actualizar el texto en un cuadro de mensaje. Si tuviera que hacer esto, probablemente aparecería otro formulario con una etiqueta y usaría un temporizador que actualiza el texto de la etiqueta en cada tic.

Aquí hay un ejemplo de código para usar un posible punto de partida:

Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $Form = New-Object system.Windows.Forms.Form $script:Label = New-Object System.Windows.Forms.Label $script:Label.AutoSize = $true $script:Form.Controls.Add($Label) $Timer = New-Object System.Windows.Forms.Timer $Timer.Interval = 1000 $script:CountDown = 60 $Timer.add_Tick( { $script:Label.Text = "Your system will reboot in $CountDown seconds" $script:CountDown-- } ) $script:Timer.Start() $script:Form.ShowDialog()

Tendrá que expandirse para satisfacer sus necesidades, como la lógica condicional para hacer cualquier acción que desee (por ejemplo, reiniciar) cuando la cuenta regresiva llegue a 0, tal vez agregar un botón para abortar, etc.

Estoy tratando de mostrar un temporizador en mi cuadro de mensaje que creé con PS Forms. Me gustaría algo así:

"Tu PC se apagará en 10 segundos" después de 1 sec.

"Tu PC se apagará en 9 segundos"

"Tu PC se apagará en 8 segundos", y así sucesivamente.

Espero que puedas ayudarme.


Hay un método PopUp de Windows Script Host, que le permite establecer un time to live para PopUp . No creo que haya una manera de no actualizar un cuadro de mensaje de PowerShell (no me cites sobre eso). Dos líneas del código vinieron de aquí .

No estoy seguro si esto es lo que quiere, pero esto funciona (solución cruda):

$timer = New-Object System.Timers.Timer $timer.AutoReset = $true #resets itself $timer.Interval = 1000 #ms $initial_time = Get-Date $end_time = $initial_time.AddSeconds(12) ## don''t know why, but it needs 2 more seconds to show right # create windows script host $wshell = New-Object -ComObject Wscript.Shell # add end_time variable so it''s accessible from within the job $wshell | Add-Member -MemberType NoteProperty -Name endTime -Value $end_time Register-ObjectEvent -SourceIdentifier "PopUp Timer" -InputObject $timer -EventName Elapsed -Action { $endTime = [DateTime]$event.MessageData.endTime $time_left = $endTime.Subtract((Get-Date)).Seconds if($time_left -le 0){ $timer.Stop() Stop-Job -Name * -ErrorAction SilentlyContinue Remove-Job -Name * -ErrorAction SilentlyContinue #other code # logoff user? } $event.MessageData.Popup("Your PC will be shutdown in $time_left sec",1,"Message Box Title",64) } -MessageData $wshell $timer.Start()

EDITAR : La solución presentada por @JonDechiro es mucho más limpia que la mía y más apropiada para lo que OP solicitó.