Challenge - 5 Problems
CmdletBinding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this advanced function call?
Consider this PowerShell advanced function with CmdletBinding. What will be the output when calling
Get-Greeting -Name 'Alice'?PowerShell
function Get-Greeting { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string]$Name ) Write-Output "Hello, $Name!" } Get-Greeting -Name 'Alice'
Attempts:
2 left
💡 Hint
Look at how the parameter is used inside the function and how the function is called.
✗ Incorrect
The function uses CmdletBinding with a mandatory parameter 'Name'. When called with -Name 'Alice', it outputs 'Hello, Alice!'.
💻 Command Output
intermediate2:00remaining
What error does this advanced function produce?
What error will this PowerShell advanced function produce when called without parameters?
PowerShell
function Test-Param {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$InputValue
)
Write-Output $InputValue
}
Test-ParamAttempts:
2 left
💡 Hint
Mandatory parameters require a value when calling the function.
✗ Incorrect
Since 'InputValue' is mandatory, calling the function without it causes an error about the missing parameter.
📝 Syntax
advanced2:00remaining
Which option correctly defines an advanced function with CmdletBinding and a switch parameter?
Select the correct PowerShell function definition that uses CmdletBinding and includes a switch parameter named 'VerboseMode'.
Attempts:
2 left
💡 Hint
CmdletBinding must be an attribute before param block, and switch parameters require [switch] type.
✗ Incorrect
Option A correctly places [CmdletBinding()] before param and declares the switch parameter with [switch]. Other options have syntax errors or misplaced attributes.
🚀 Application
advanced2:00remaining
What is the output of this advanced function using Begin, Process, and End blocks?
Given this advanced function, what will be the output when calling
Process-Numbers -Numbers 1,2,3?PowerShell
function Process-Numbers { [CmdletBinding()] param( [int[]]$Numbers ) begin { Write-Output 'Starting processing' } process { foreach ($num in $Numbers) { Write-Output ($num * 2) } } end { Write-Output 'Processing complete' } } Process-Numbers -Numbers 1,2,3
Attempts:
2 left
💡 Hint
Begin runs once before processing, Process runs for each input, End runs after all processing.
✗ Incorrect
The function writes 'Starting processing' once, then outputs each number doubled, then writes 'Processing complete'.
🔧 Debug
expert3:00remaining
Why does this advanced function fail to bind the parameter correctly?
This function is intended to accept pipeline input by property name. Why does it fail to bind the parameter when called with pipeline input?
PowerShell
function Get-UserName { [CmdletBinding()] param( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)] [string]$UserName ) process { Write-Output "User: $UserName" } } # Pipeline input $users = @(@{UserName='Bob'}, @{UserName='Carol'}) $users | Get-UserName
Attempts:
2 left
💡 Hint
Consider how PowerShell binds pipeline input by property name and the type of objects passed.
✗ Incorrect
Hashtable objects do not have properties accessible by property name binding. PowerShell expects objects with properties, not hashtables, for ValueFromPipelineByPropertyName to work.