Bird
0
0

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?
A[CmdletBinding()] param( [Parameter(Mandatory=$true)] [ValidateSet('Start','Stop','Restart')] [string]$Action )
Bparam( [Parameter()] [ValidateSet('Start','Stop','Restart')] [string]$Action = 'Start' )
C[CmdletBinding()] param( [Parameter()] [ValidateSet('Start','Stop','Restart')] [string]$Action )
Dparam( [Parameter(Mandatory=$true)] [string]$Action )
Step-by-Step Solution
Solution:
  1. Step 1: Ensure Mandatory prompts user

    To prompt the user when a parameter is missing, the script must use CmdletBinding() and set Mandatory=$true.
  2. Step 2: Restrict values with ValidateSet

    Using ValidateSet limits input to the allowed values 'Start', 'Stop', or 'Restart'.
  3. 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.
  4. Final Answer:

    [CmdletBinding()] param( [Parameter(Mandatory=$true)] [ValidateSet('Start','Stop','Restart')] [string]$Action ) -> Option A
  5. Quick Check:

    CmdletBinding + Mandatory + ValidateSet = [CmdletBinding()] param( [Parameter(Mandatory=$true)] [ValidateSet('Start','Stop','Restart')] [string]$Action ) [OK]
Quick Trick: Use CmdletBinding with Mandatory and ValidateSet together [OK]
Common Mistakes:
  • Omitting CmdletBinding() so no prompt occurs
  • Setting default value disables Mandatory prompt
  • Not using ValidateSet to restrict values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes