Last active
February 12, 2018 21:17
-
-
Save fluttr/f0283eb0f133e6aa0264977956657a73 to your computer and use it in GitHub Desktop.
Waits $TimeoutSeconds for user input and then returns input if any.
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
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" | |
break | |
} | |
} | |
if($input){ | |
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]','' | |
if($customPrefix){ | |
$reportDir = Split-Path -Parent $ReportFileName | |
$reportSuffix = Split-Path -Leaf $ReportFileName | |
$ReportFileName = "$reportDir\${customPrefix}_$reportSuffix" | |
} | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[console]::KeyAvailable does not working in PowerShell ISE. Try it in real console.