Bird
0
0

Identify the error in this parameter validation code:

medium📝 Debug Q14 of 15
PowerShell - Scripting Best Practices
Identify the error in this parameter validation code:
function Set-Name {
  param(
    [ValidateNotNullOrEmpty()]
    [string]$Name
  )
  "Name set to $Name"
}
Set-Name -Name $null
AThe parameter type should be [int] instead of [string]
BThe function will throw a validation error because $Name is null
CThe ValidateNotNullOrEmpty attribute is used incorrectly without parameters
DThe function will accept null and print 'Name set to '
Step-by-Step Solution
Solution:
  1. Step 1: Understand ValidateNotNullOrEmpty behavior

    This attribute rejects null or empty string inputs.
  2. Step 2: Analyze input value

    The input is $null, which violates the validation rule, causing an error.
  3. Final Answer:

    The function will throw a validation error because $Name is null -> Option B
  4. Quick Check:

    Null input with ValidateNotNullOrEmpty = error [OK]
Quick Trick: Null or empty inputs fail ValidateNotNullOrEmpty [OK]
Common Mistakes:
  • Thinking null is accepted
  • Assuming ValidateNotNullOrEmpty needs parameters
  • Confusing parameter type requirements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes