Bird
0
0

How would you correctly define a mandatory parameter named Environment that only accepts the values 'Dev', 'Test', or 'Prod' in a PowerShell function?

easy📝 Syntax Q3 of 15
PowerShell - Functions
How would you correctly define a mandatory parameter named Environment that only accepts the values 'Dev', 'Test', or 'Prod' in a PowerShell function?
A[Parameter(Mandatory=$true)] [ValidateRange('Dev','Test','Prod')] [string]$Environment
B[Parameter()] [ValidateSet('Dev','Test','Prod')] [string]$Environment
C[Parameter(Mandatory=$false)] [ValidateSet('Dev','Test','Prod')] [int]$Environment
D[Parameter(Mandatory=$true)] [ValidateSet('Dev','Test','Prod')] [string]$Environment
Step-by-Step Solution
Solution:
  1. Step 1: Use Mandatory attribute

    Set Mandatory=$true to require the parameter.
  2. Step 2: Use ValidateSet for allowed values

    [ValidateSet('Dev','Test','Prod')] restricts input to these strings.
  3. Step 3: Correct data type

    Use [string] since values are strings.
  4. Final Answer:

    [Parameter(Mandatory=$true)] [ValidateSet('Dev','Test','Prod')] [string]$Environment -> Option D
  5. Quick Check:

    Mandatory + ValidateSet with correct type [OK]
Quick Trick: Mandatory with ValidateSet restricts input values [OK]
Common Mistakes:
  • Omitting Mandatory attribute
  • Using ValidateRange instead of ValidateSet
  • Incorrect data type for string values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes