0
0
PowerShellscripting~20 mins

Parameter validation in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Parameter Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell function call with parameter validation?
Consider the following PowerShell function that validates its parameter. What will be the output when calling Test-Param -Number 15?
PowerShell
function Test-Param {
  param(
    [ValidateRange(1,10)]
    [int]$Number
  )
  "Number is $Number"
}

Test-Param -Number 15
ANo output, function silently ignores invalid input
BNumber is 15
CValidationException: Cannot validate argument on parameter 'Number'. The argument 15 is less than the minimum allowed range of 1.
DValidationException: Cannot validate argument on parameter 'Number'. The argument 15 is greater than the maximum allowed range of 10.
Attempts:
2 left
💡 Hint
Think about what happens when a parameter value is outside the allowed range in PowerShell parameter validation.
💻 Command Output
intermediate
2:00remaining
What error does this PowerShell function produce when called with an invalid string?
Given this function with parameter validation, what error message appears when calling Check-Name -Name 'John123'?
PowerShell
function Check-Name {
  param(
    [ValidatePattern('^[a-zA-Z]+$')]
    [string]$Name
  )
  "Name is $Name"
}

Check-Name -Name 'John123'
AValidationException: Cannot validate argument on parameter 'Name'. The argument 'John123' does not match the pattern '^[a-zA-Z]+$'.
BNo output, function returns null
CRuntimeException: Invalid characters in parameter 'Name'.
DName is John123
Attempts:
2 left
💡 Hint
Look at the regex pattern used in ValidatePattern and what input is given.
📝 Syntax
advanced
2:00remaining
Which option correctly uses ValidateSet to restrict parameter values?
You want to create a function parameter that only accepts the values 'Red', 'Green', or 'Blue'. Which of the following parameter declarations is correct?
PowerShell
function Set-Color {
  param(
    # Parameter declaration here
  )
  "Color set to $Color"
}
A
[ValidateSet('Red','Green','Blue')]
[int]$Color
B
[ValidateSet('Red','Green','Blue')]
[string]$Color
C
[ValidateSet('Red','Green','Blue')]
[bool]$Color
D
[ValidateSet('Red','Green','Blue')]
[array]$Color
Attempts:
2 left
💡 Hint
Think about the data type that matches color names.
🚀 Application
advanced
2:00remaining
What is the output of this function when called with -Age 25?
This function uses multiple parameter validations. What will it output when called as Check-Age -Age 25?
PowerShell
function Check-Age {
  param(
    [ValidateRange(18,30)]
    [ValidateNotNull()]
    [int]$Age
  )
  if ($Age -ge 21) {
    "Adult"
  } else {
    "Young Adult"
  }
}

Check-Age -Age 25
AAdult
BYoung Adult
CValidationException: Age is out of range
DNo output
Attempts:
2 left
💡 Hint
Check the range and the if condition inside the function.
🧠 Conceptual
expert
2:00remaining
Which parameter validation attribute prevents null or empty strings?
You want to ensure a string parameter is not null or empty in your PowerShell function. Which validation attribute should you use?
A[ValidateNotNull()]
B[ValidateNotEmpty()]
C[ValidateNotNullOrEmpty()]
D[ValidateString()]
Attempts:
2 left
💡 Hint
One attribute specifically checks for both null and empty strings.