Bird
0
0

What is wrong with this advanced function that uses parameter validation?

medium📝 Debug Q7 of 15
PowerShell - Functions
What is wrong with this advanced function that uses parameter validation?
function Test-Number {
  [CmdletBinding()]
  param(
    [ValidateRange(1,10)]
    [int]$Number
  )
  Write-Output "Number is $Number"
}
Calling Test-Number -Number 15 results in:
AThe function outputs 'Number is 15' without error
BThe function throws a validation error
CThe function ignores the parameter and uses default
DThe function crashes with a syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand ValidateRange attribute

    ValidateRange restricts the parameter value to between 1 and 10 inclusive.
  2. Step 2: Analyze input value 15

    Since 15 is outside the valid range, PowerShell throws a validation error before running the function body.
  3. Final Answer:

    The function throws a validation error -> Option B
  4. Quick Check:

    ValidateRange enforces parameter limits strictly [OK]
Quick Trick: ValidateRange blocks invalid parameter values [OK]
Common Mistakes:
  • Expecting function to run with invalid parameter
  • Thinking ValidateRange sets default values
  • Confusing validation errors with syntax errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes