Bird
0
0

What is wrong with this parameter validation?

medium📝 Debug Q7 of 15
PowerShell - Scripting Best Practices
What is wrong with this parameter validation?
function Set-Name {
  param(
    [ValidateLength(3,10)]
    [int]$Name
  )
  Write-Output $Name
}
AValidateLength requires three arguments
BValidateLength cannot be applied to integers
CMissing ValidateNotNullOrEmpty attribute
DParameter name is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Understand ValidateLength usage

    [ValidateLength()] applies only to strings to check their length.
  2. Step 2: Check parameter type

    Parameter $Name is typed as [int], so applying ValidateLength causes an error.
  3. Final Answer:

    ValidateLength cannot be applied to integers -> Option B
  4. Quick Check:

    ValidateLength works only on strings [OK]
Quick Trick: Use ValidateLength only with string parameters [OK]
Common Mistakes:
  • Applying string validation to numbers
  • Assuming ValidateLength needs more args
  • Ignoring type mismatch errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes