0

In Command Prompt, I'd throw the following into an iterated loop:

choice /c:abcdefghijklmnopqrstuvwxyz1234567890 /n /t:1 /d:0 
sleep 1

In Unix, I'd use: read -t 5 $input.

Does anyone know a way to do it in Powershell? Right now, I just wait it out (last line below).

I've set my script to switch between the TV and PC for my wife. I print a before and after for my wife and I'd like to bypass it for myself.



Here is my current script for context:

 $file_location = "C:\Users\krmar_000\Documents\audio.out"
 $current = gc $file_location
 Write-Host "Current Audio output: " -ForegroundColor White -NoNewline
 Write-Host $current -ForegroundColor Red

 Write-Host "Changing..." -ForegroundColor Yellow

 if ( $current -eq "Speakers” )
 {
     nircmd.exe mutesysvolume 0
     nircmd.exe setsysvolume 65535

     nircmd.exe setdefaultsounddevice “AMD HDMI Output”
     "AMD HDMI Output" | Out-File -filepath $file_location
 #    [System.Windows.Forms.MessageBox]::Show("TV Speakers enabled","Speaker Flip")

     Write-Host "TV Speakers enabled" -ForegroundColor Green
 }
 elseif ( $current -eq "AMD HDMI Output” )
 {
     nircmd.exe mutesysvolume 0
     nircmd.exe setsysvolume 52428

     nircmd.exe setdefaultsounddevice “Headset”
     "Speakers" | Out-File -filepath $file_location
 #    [System.Windows.Forms.MessageBox]::Show("Headset Speakers enabled","Speaker Flip")

     Write-Host "Headset Speakers enabled" -ForegroundColor Green
 }
 else
 {
     nircmd.exe setdefaultsounddevice “Speakers”
     "Speakers" | Out-File -filepath $file_location
 #    [System.Windows.Forms.MessageBox]::Show("Headset Speakers enabled","Speaker Flip")    

     Write-Host "Headset Speakers enabled" -ForegroundColor Green 
 }
 Start-Sleep -Seconds 5
krmarshall87
  • 191
  • 2
  • 9
  • 2
    Does this address your issue? http://stackoverflow.com/questions/150161/waiting-for-user-input-with-a-timeout or http://thecuriousgeek.org/?p=253 – Matt Dec 13 '14 at 04:46
  • At least from my test, this appears to wait for x seconds before accepting input. I'd like to read input OR timeout after x seconds. – krmarshall87 Dec 13 '14 at 14:40
  • First, just pauses/sleeps. Second errors on `$key = $host.ui.rawui.readkey("NoEcho,IncludeKeyUp")` – krmarshall87 Dec 13 '14 at 14:58

1 Answers1

0

It's not exactly a dupe of the linked answer, but it's pretty close. Full function here (based loosely on same) for convenience:

function Read-HostWithDelay {
  param([Parameter(Mandatory=$true)][int]$Delay, [string]$Prompt, [Switch]$AsSecureString)
  [int]$CSecDelayed = 0
  do {
    [bool]$BReady = $host.UI.RawUI.KeyAvailable
    [Threading.Thread]::Sleep(1000)
  } while (!$BReady -and ($CSecDelayed++ -lt $Delay))
  if ($BReady -and $Prompt) { Read-Host $Prompt -AsSecureString:$AsSecureString }
  # No, Read-Host will not in fact handle null parameter binding (-Prompt:$Prompt) properly. Who knows why not.
  elseif ($BReady) { Read-Host -AsSecureString:$AsSecureString }      
}

Tested, and a good thing too, as it proved just a touch less trivial than I'd frankly expected.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
  • At least from my test, this appears to wait for x seconds before accepting input. I'd like to read input OR timeout after x seconds. – krmarshall87 Dec 13 '14 at 14:54
  • 1
    Just doublechecked, @krmarshall87, and on PowerShell 4.0 it seems to have decent timing response; setting a 50-second delay and typing immediately makes me wait 1-2 seconds before characters appear. – Nathan Tuggy Dec 13 '14 at 15:21
  • Thanks. I'll check when I get back in the office in a few days. I may be using v3. Appreciate it! – krmarshall87 Dec 14 '14 at 18:10