A PowerShell script to calculate exercise training heart rate zones. It is intended for use by non-technical users. For this reason the design criteria were selected with the needs of consumers in mind.
  1. To support the most clients, the script should run under PowerShell version 1, if at all possible. The script should also run under newer versions without problems.
  2. The script should run on platforms such as Windows 8.1 RT, where PowerShell is constrained.
  3. Where possible, the script should present a GUI interactive interface.
  4. The script should also support clients where Windows Forms are not allowed, or where the user requests a command line interface.
  5. The training heart rate zones should be output at the command line, so the output can be redirected to a text file.
  6. THe user should be able to select from several formulas to estimate the maximum heart rate based on their sex and age.
Besides providing useful results, the script also demonstrates the following:
  1. How to specify script parameters.
  2. Techniques for validating the input values.
  3. How to prompt the user for valid input.
  4. Providing help for the script.
  5. How to test if PowerShell is constrained.
  6. How to use Windows Forms and various controls in PowerShell.
  7. How to make the GUI interactive, using controls that implement code.
  8. How to make various controls only visible when appropriate.
  9. How to recognize when the Enter or Escape keys are pressed and run code on these events.
The training heart rate calculations are based on the Karvonen method, as described in the book "Serious Training for Serious Athletes", by Rob Sleamaker, Leisure Press, 1989, pages 68-69. The formulas for estimating the maximum heart rate are based mostly on the Wikipedia page for Heart Rate (as of 8/25/2016): https://en.wikipedia.org/wiki/Heart-rate
The script incorporates the following features:
  1. Functions are used in the script so that formulas can be modified if desired, perhaps based on more recent information.
  2. Variables are defined for validating input values, so they can be modified if desired. The values only need to be changed in one place, and all code and output are adjusted for the new limits.
  3. Many comments are included in the code to help understand the logic and explain the reasoning.

When the Windows form is shown, only code on the form can be run. It is important to have all necessary code defined in the controls, which run on events, such as button clicks. However, the code can call functions that have been defined elsewhere in the script. But a lot of testing is necessary to make sure the values necessary are passed to the functions. If necessary, variables that are assigned values in these functions must be defined to have Script or Global scope.

The parameters that can be passed to the script are:
    -RestingHR: Resting heart rate in beats per minute. An integer between 28 and 100. If no value is provided you will be prompted for a value.
    -MaxHR: Maximum heart rate in beats per minute. An integer between 100 and 215. If no value is provided, an estimate will be calculated based on your sex and age.
    -Sex: Your sex, among one of the following: Male, M, Female, F. Only needed if you do not provide a maximum heart rate, or you use the -Estimate parameter.
    -Age: Your age in years. An integer between 5 and 105. Only needed if you do not provide a maximum heart rate, or you use the -Estimate parameter.
    -Formula: The formula to use to estimate the maximum heart rate (if needed).
        Options: Oakland (default), Haskell, Cooper, Tanaka, Miller, Londeree, Gellish, or Robergs.
    -Estimate: A switch indicating that you want the script to calculate an estimated maximum heart rate. You are prompted for your sex (if you did not provide the -Sex parameter) and your age (if you did not provide the -Age parameter).
    -NoGUI: A switch that requests that the GUI form not be used. Data input is accepted and all results are displayed at the command line.
    -Black: A switch to output results without using the Write-Host cmdlet, so the output can be redirected to a file.
    -Help: A switch that outputs help information.
Note: The first one (or several) letters of each parameter can be used as aliases.
    In addition, the parameters -RestingHR, -MaxHR, -Sex, -Age, and -Formula can be listed in order without the parameter names. See the first example below.

Some usage examples:
.\HRZones.ps1 52 180
    Calculate heart rate zones based on a resting HR of 52 and a maximum HR of 180.
.\HRZones.ps1 -MaxHR 180 -RestingHR 52
    Same as the previous.
.\HRZones.ps1 52 180 m 40 Cooper -est
    Calculate zones based on a resting HR of 52 and estimated maximum using Cooper formula
    and a 40 year old male. The 180 maximum HR will not be used.
.\HRZone.ps1 -Estimate -Sex F -Age 39 -RestingHR 52
    Calculate heart rate zones based on a resting HR of 52 and an estimated maximum.
    The maximum heart rate will be calculated based on a 39 year old female.
.\HRZones.ps1 -r 52 -s F -a 39 -e -n
    Same as the previous, but no GUI is selected.
.\HRZones.ps1 -R 52
    You are prompted for either your maximum heart rate, or your sex and age.
    You are prompted for resting heart rate and either maximum heart rate or sex and age.

The following image shows the form and all of the controls on the form. Note that there is no situation where all of these controls would be visible at once, so the code was modified to show them all.
The controls on the Windows form (HRZForm), listed from left to right, and from top to bottom, are as follows:
Control Class
$Warning Label
$RestingLabel Label
$Resting TextBox
$RestingFlag Label
$MaxLabel Label
$Max TextBox
$MaxFlag Label
$EstimateLabel Label
$EstimateChk CheckBox
$FormualLabel Label
$Formula1 RadioButton
$Formula2 RadioButton
$Formula3 RadioButton
$Formula4 RadioButton
$Formula5 RadioButton
$Formula6 RadioButton
$Formula7 RadioButton
$Formula8 RadioButton
$SexLabel Label
$SexBox TextBox
$SexFlag Label
$AgeLabel Label
$AgeBox TextBox
$AgeFlag Label
$EstimateMaxLabel Label
$EstimateMax Label
$ReportLabel Label
$L0Label Label
$L0BPM Label
$L1Label Label
$L1BPM Label
$L2Label Label
$L2BPM Label
$L3Label Label
$L3BPM Label
$L4Label Label
$L4BPM Label
$L5Label Label
$L5BPM Label
$Calculate Button
$Report Button
$Cancel Button

 A batch file can be used to launch the script and pass up to 9 arguments. This can help non-technical people use the script without the need to know how to launch PowerShell. The batch file would be similar to below (named HRZones.bat):


Windows Shell Script
@echo off 
PowerShell -Command "& {.\HRZones.ps1 %1 %2 %3 %4 %5 %6 %7 %8 %9}" 
 This syntax is supported in PowerShell V1 and later versions. Only a maximum of 9 arguments (parameters and values) can be passed to the script. For example, the script can be launched with the following at a command prompt, if the user is in the directory where HRZones.bat is saved.
Windows Shell Script
HRZones -r 52 -est -a 40 -s f -f Cooper
 This example uses the maximum number of arguments (9).