Which of the following parameter declarations correctly enforces both requirements and will prompt the user if the parameter is missing?
hard📝 Application Q15 of 15
PowerShell - Functions
You want to create a script parameter that is required and only accepts the values 'Start', 'Stop', or 'Restart'. Which of the following parameter declarations correctly enforces both requirements and will prompt the user if the parameter is missing?
To prompt the user when a parameter is missing, the script must use CmdletBinding() and set Mandatory=$true.
Step 2: Restrict values with ValidateSet
Using ValidateSet limits input to the allowed values 'Start', 'Stop', or 'Restart'.
Step 3: Check all options
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateSet('Start','Stop','Restart')]
[string]$Action
) includes CmdletBinding(), Mandatory=$true, and ValidateSet, fulfilling all requirements.
Final Answer:
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateSet('Start','Stop','Restart')]
[string]$Action
) -> Option A