Bird
0
0

You want to create a script parameter $Port that only accepts integers between 1024 and 65535 and cannot be empty. Which parameter validation attributes should you use together?

hard📝 Application Q15 of 15
PowerShell - Scripting Best Practices
You want to create a script parameter $Port that only accepts integers between 1024 and 65535 and cannot be empty. Which parameter validation attributes should you use together?
A[ValidateRange(1024,65535)][ValidateNotNullOrEmpty()]
B[ValidateSet(1024,65535)][ValidateNotNullOrEmpty()]
C[ValidatePattern('^[0-9]{4,5}$')]
D[ValidateNotNullOrEmpty()][ValidateLength(4,5)]
Step-by-Step Solution
Solution:
  1. Step 1: Choose attribute for numeric range

    [ValidateRange(1024,65535)] ensures the integer is within the port number range.
  2. Step 2: Ensure parameter is not empty

    [ValidateNotNullOrEmpty()] prevents null or empty input.
  3. Step 3: Combine both for full validation

    Using both attributes together enforces the correct range and non-empty input.
  4. Final Answer:

    [ValidateRange(1024,65535)][ValidateNotNullOrEmpty()] -> Option A
  5. Quick Check:

    Range + NotNullOrEmpty = correct port validation [OK]
Quick Trick: Combine ValidateRange and ValidateNotNullOrEmpty for numeric required input [OK]
Common Mistakes:
  • Using ValidateSet for numeric ranges
  • Relying on ValidatePattern for numeric range checks
  • Missing non-empty validation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes