Bird
0
0

Which parameter validation attributes should you use together?

hard📝 Application Q8 of 15
PowerShell - Scripting Best Practices
You want to create a function that accepts a parameter Mode which must be either 'Auto', 'Manual', or 'Semi'. Additionally, the parameter should never be empty or null. Which parameter validation attributes should you use together?
A[ValidatePattern('Auto|Manual|Semi')][ValidateLength(1,10)]
B[ValidateRange('Auto','Semi')][ValidateNotNullOrEmpty()]
C[ValidateSet('Auto','Manual','Semi')][ValidateLength(11,20)]
D[ValidateSet('Auto','Manual','Semi')][ValidateNotNullOrEmpty()]
Step-by-Step Solution
Solution:
  1. Step 1: Choose attribute for allowed values

    [ValidateSet('Auto','Manual','Semi')] restricts values to these three options.
  2. Step 2: Ensure parameter is not empty or null

    [ValidateNotNullOrEmpty()] prevents empty or null inputs.
  3. Final Answer:

    [ValidateSet('Auto','Manual','Semi')][ValidateNotNullOrEmpty()] -> Option D
  4. Quick Check:

    Combine ValidateSet and ValidateNotNullOrEmpty for fixed non-empty values [OK]
Quick Trick: Combine ValidateSet and ValidateNotNullOrEmpty to restrict and require input [OK]
Common Mistakes:
  • Using ValidateRange for strings
  • Using ValidateLength without null check
  • Confusing regex with ValidateSet

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes