So I want to have a Read-Host (or anything else that is capable of reading input) and a Timeout.
For example:
The user is asked if he wants to continue the script.
If he types “Yes” it will continue.
If he types “No” the script will end
If no input is made in 20 seconds, the script will continue.
Thanks for your help!
Already tried:
$sleepSeconds = 10
$timeout = New-TimeSpan -Seconds $sleepSeconds
$stopWatch = [Diagnostics.Stopwatch]::StartNew()
while ($stopWatch.Elapsed -lt $timeout)
{
if ($Host.UI.RawUI.KeyAvailable)
{
$keyPressed = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyUp, IncludeKeyDown")
Write-Host "Abbruch"
if ($keyPressed.KeyDown -eq "True") { break }
}
}
New contributor
Leon Schock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.