Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Waits $TimeoutSeconds for user input and then returns input if any.
function waitForUserInput([parameter(mandatory=$true)][int]$TimeoutSeconds, [string]$Prompt){
$counter = 0
while($counter++ -lt $TimeoutSeconds){
if(-not [console]::KeyAvailable){
Start-Sleep -Seconds 1
} else {
$input = Read-Host -Prompt "$Prompt"
Write-Output "$input"
$ReportFileName = 'c:\whatever\some.file'
$UserInputTimeoutSeconds = 3
# ask user for custom report prefix
echo "Press any key in $UserInputTimeoutSeconds seconds to specify custom report name..."
$customPrefix = waitForUserInput -TimeoutSeconds $UserInputTimeoutSeconds -Prompt "Report prefix"
$customPrefix = $customPrefix -replace '[^\w]',''
$reportDir = Split-Path -Parent $ReportFileName
$reportSuffix = Split-Path -Leaf $ReportFileName
$ReportFileName = "$reportDir\${customPrefix}_$reportSuffix"
# ...

This comment has been minimized.

Copy link
Owner Author

@fluttr fluttr commented Feb 12, 2018

[console]::KeyAvailable does not working in PowerShell ISE. Try it in real console.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.