A couple of PowerShell functions for getting a quick single key response from a user for “Yes/No” and “Press any key to Continue” scenarios with options for the prompt and default response, as part of the djaus2/az-iothub-ps tool (available on GitHub).

About az-iothub-ps

This tool is a menu driven PowerShell tool that can create an Azure Group, IoT Hub, Device and Device Provisioning Service (both linked to the hub). There is also an option to automatically create all those items from the command line. It automatically makes all of the Azure IoT Hub SDK queries.

About

The function get-anykey when called in PowerShell, waits for the user to press any key on the keyboard. It is essenatially a user controlled pause, typically so that they can read some displayed text. If the text is short, then that can be part of the function call, in which case it is displayed by the function as part of its prompt.There is a default prompt of ' Press any key to return.. This can be appended to an incoming prompt. The word return can be modified to another term such as ```continue``. The key that is pressed is ignored…“Doesn’t matter!”

function get-anykey{
param (
[string]$Prompt = 'Press any key to return.',
[string]$Mode ='',
[boolean]$TimeStamp =$true
)
     ...
     ...
}

If $TimeStamp is true then when called it displays the current elapsed time from when the clcok was initiated by another function call elsewhere. This is used when running automated tasks and for when recoding the tool’s use.

The function get-yesorno is similar in structure and function to get-anykey except that the key pressed is examined. A case insensitive keypress of Y means $true or Yes, and N means $false or No. A keypress of return or any other key returns the $Default value. By default this is $false. The default value can be supplied as the first parameter of the function call.

function get-yesorno{
param (
    [boolean]$Default = $false,
    [string]$Prompt = 'Select one of [Y]es [N]o'
    )
    ...
    ...
}

Redirected mode

This is determined by the environment variable which can be set: $env:IsRedirected. This is set for when the script is run in, for example, a remote PowerShell into an Windows 10 IoT-Core device (eg the RPi). For this, like for PowerShell ISE, [System.Console]::ReadKey($true) does not work. A read-host call is used (which requires a [Return] keypress), with the first character being used for the keypress. A blank string is interpretted as a return.

Usage examples

get-anykey

If ([string]::IsNullOrEmpty($GroupName ))
{
    write-Host ''
    $prompt = 'Need to select a Group first.'
    get-anykey $prompt
    return
}

This will display:

Need to select a Group first
 Press any key to return.

get-yesorno

  $DeviceName='MyDevice'
  $prompt =  'Do you want to delete the Device "' + $DeviceName +  '"'
  write-Host $prompt
  get-yesorno $false
  $answer = $global:retVal
  if (-not $answer)
  {
      return
  }

This will display:

Do you want to delete the Device "MyDevice"
Select one of [Y]es [N]o

Alternatively, the function call could have included the prompt:

  $DeviceName='MyDevice'
  $prompt =  'Do you want to delete the Device "' + $DeviceName +  '"'
  get-yesorno $false $prompt
  $answer = $global:retVal
  if (-not $answer)
  {
      return
  }

Automated Mode

There is an automated mode not requiring a keypress. If $global:yesnowait exists then the function pauses for the period specified by $global:yesnowait seconds then returns. the functionget-yesorno returns the $Default value. This is used when the tool runs from the command line in an automated mode, without user intervetion after a few inputs are given. There is also $global:recording and $global:recordingTime for when the tool’s usage is being recorded. The function delays for the specified period so that a watcher of the video can read the prompt.


 TopicSubtopic
  Next: > PowerShell
<  Prev:   PowerShell
This Category:Coding
  Next: > PowerShell show-image A PS Function to Display an Image in a Windows Form 
<  Prev:   PowerShell