Recall & Review
beginner
What is parameter validation in PowerShell functions?
Parameter validation ensures that the inputs to a function meet specific rules before the function runs. It helps catch errors early and makes scripts more reliable.
Click to reveal answer
beginner
How do you enforce that a parameter must not be empty in PowerShell?
Use the [ValidateNotNullOrEmpty()] attribute before the parameter. It stops the function if the parameter is null or an empty string.
Click to reveal answer
beginner
What does the [ValidateRange(min, max)] attribute do?
It checks that a numeric parameter is within the specified minimum and maximum values. If the input is outside this range, PowerShell throws an error.
Click to reveal answer
beginner
How can you restrict a parameter to accept only specific values?
Use the [ValidateSet('value1', 'value2', ...)] attribute. This limits the parameter to only those listed values, helping avoid typos or invalid inputs.
Click to reveal answer
beginner
What happens if a parameter fails validation in PowerShell?
PowerShell stops running the function and shows an error message explaining which validation rule was broken. This prevents the function from running with bad input.
Click to reveal answer
Which attribute ensures a parameter is not null or empty in PowerShell?
✗ Incorrect
The [ValidateNotNullOrEmpty()] attribute checks that the parameter is neither null nor an empty string.
What does [ValidateSet('Red','Green','Blue')] do for a parameter?
✗ Incorrect
It restricts the parameter to accept only the specified values 'Red', 'Green', or 'Blue'.
If a parameter has [ValidateRange(1,10)], what happens if you pass 15?
✗ Incorrect
Passing a value outside the range causes PowerShell to throw a validation error and stop the function.
Which attribute would you use to ensure a string parameter is not empty?
✗ Incorrect
Use [ValidateNotNullOrEmpty()] to ensure the string parameter is not empty or null.
What is the main benefit of parameter validation in scripts?
✗ Incorrect
Parameter validation helps catch bad inputs early, preventing errors during script execution.
Explain how to use parameter validation attributes in a PowerShell function and why they are useful.
Think about how you can stop a function from running if the input is wrong.
You got /4 concepts.
Describe what happens when a parameter fails validation in PowerShell and how it affects the function execution.
Consider what PowerShell does to protect your script from bad inputs.
You got /4 concepts.