Instead of using pause
in scripts, it is useful to me to make a script auto-exit after a set time. This is particularly useful to me as various of these scripts can run interactively or as a background task with Start-Process -WindowStyle Hidden
where a pause
would mean that process could hang around forever, but with the timer, even if it's in the background, it will time out. I was given this solution via this question: PowerShell, break out of a timer with custom keypress
$t = 8; Write-Host "Exiting in $t seconds (press any key to exit now)" -NoNewLine
for ($i=0; $i -le $t; $i++) {
Start-Sleep 1; Write-Host -NoNewLine "."
if ([Console]::KeyAvailable) {
$key = [Console]::ReadKey($true).Key
if ($key) { break }
}
}
However, if I run this with Start-Process -WindowStyle Hidden
in the background, PowerShell generates an error due to [Console]::KeyAvailable
as there is no console available when running in the background.
Cannot see if a key has been pressed when either application does not have a
console or when console input has been redirected from a file. Try
Console.In.Peek.
At C:\Users\Boss\Install Chrome.ps1:36 char:78
+ ... ep 1; Write-Host -NoNewLine "."; if ([Console]::KeyAvailable) { $key ...
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], InvalidOperationExcept
ion
+ FullyQualifiedErrorId : System.InvalidOperationException
• Is there some way that I can adjust the code I have such that it will not generate an error when run in the background, while still retaining the "press any key to exit now" option when run interactively?
• What does Try Console.In.Peek
mean?