Created
May 9, 2019 08:10
-
-
Save pcrockett-pathway/4ec3ba0a716c6b677caad003b7f4b4be to your computer and use it in GitHub Desktop.
Example of a PowerShell loop that a user can "escape" with a keystroke
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [CmdletBinding()] | |
| param( | |
| [Parameter()] | |
| [int]$PollFrequencySeconds = 2 | |
| ) | |
| $ErrorActionPreference = "Stop" | |
| Set-StrictMode -Version 5.0 | |
| $stopKey = "Q" | |
| Write-Host "Press $stopKey to stop." | |
| function userWantsToStop() { | |
| while ([Console]::KeyAvailable) { | |
| if ([Console]::ReadKey($true).Key -eq $stopKey) { | |
| return $true | |
| } | |
| } | |
| return $false | |
| } | |
| while (!(userWantsToStop)) { | |
| # Do work here | |
| Start-Sleep -Seconds $PollFrequencySeconds | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment