Tengo un script que inicia un evento y espera a que el usuario presione cualquier tecla para detener la ejecución del script. Estaba intentando encontrar una manera de mostrar un temporizador (cuánto tiempo ha estado ejecutándose el script) mientras esperaba la entrada del usuario en Read-Host. ¿Hay alguna forma de lograr esto?Powershell mostrar el tiempo transcurrido
Editar: Gracias por toda la aportación. Esto funciona
$Time = [System.Diagnostics.Stopwatch]::StartNew()
while ($true) {
$CurrentTime = $Time.Elapsed
write-host $([string]::Format("`rTime: {0:d2}:{1:d2}:{2:d2}",
$CurrentTime.hours,
$CurrentTime.minutes,
$CurrentTime.seconds)) -nonewline
sleep 1
if ($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyUp,NoEcho").Character)) {
Write-Host "Exiting now"
break;
}
}
Echa un vistazo aquí para usar la interfaz de usuario de WPF en backgound en powershell http://www.nivot.org/blog/post/2008/05/23/BackgroundTimerPowerShellWPFWidget –