0
0
PowerShellscripting~20 mins

Advanced functions (CmdletBinding) in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CmdletBinding 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 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'
AHello, $Name!
BError: Missing mandatory parameter 'Name'
CHello, Alice!
DHello, !
Attempts:
2 left
💡 Hint
Look at how the parameter is used inside the function and how the function is called.
💻 Command Output
intermediate
2: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-Param
AError: Missing mandatory parameter 'InputValue'
BNo output, function runs silently
CError: Unexpected token 'Test-Param'
DOutput: null
Attempts:
2 left
💡 Hint
Mandatory parameters require a value when calling the function.
📝 Syntax
advanced
2: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'.
A
function Show-Info {
  [CmdletBinding()]
  param(
    [switch]$VerboseMode
  )
  if ($VerboseMode) { Write-Output 'Verbose mode on' }
}
B
function Show-Info {
  param(
    [CmdletBinding()]
    [switch]$VerboseMode
  )
  if ($VerboseMode) { Write-Output 'Verbose mode on' }
}
C
function Show-Info {
  [CmdletBinding]
  param(
    [switch]$VerboseMode
  )
  if ($VerboseMode) { Write-Output 'Verbose mode on' }
}
D
function Show-Info {
  [CmdletBinding()]
  param(
    switch $VerboseMode
  )
  if ($VerboseMode) { Write-Output 'Verbose mode on' }
}
Attempts:
2 left
💡 Hint
CmdletBinding must be an attribute before param block, and switch parameters require [switch] type.
🚀 Application
advanced
2: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
A[1, 2, 3]
B[2, 4, 6]
C[Starting processing, Processing complete]
D[Starting processing, 2, 4, 6, Processing complete]
Attempts:
2 left
💡 Hint
Begin runs once before processing, Process runs for each input, End runs after all processing.
🔧 Debug
expert
3: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
AThe parameter name 'UserName' does not match the property name in the pipeline objects.
BThe pipeline objects are hashtables, which do not support property binding as expected.
CThe function does not declare the parameter as accepting pipeline input by property name correctly.
DThe function lacks a 'begin' block to initialize pipeline processing.
Attempts:
2 left
💡 Hint
Consider how PowerShell binds pipeline input by property name and the type of objects passed.