Bird
0
0

What is wrong with this function if it does not output the piped input when piped input is given?

medium📝 Debug Q14 of 15
PowerShell - Functions
What is wrong with this function if it does not output the piped input when piped input is given?
function Test-Pipe {
  param([Parameter(ValueFromPipeline=$true)] $item)
  begin { Write-Output 'Start' }
  end { Write-Output 'End' }
}
And then called as: 1..3 | Test-Pipe
AMissing <code>process</code> block to handle pipeline input
BParameter should not have ValueFromPipeline attribute
CUsing <code>begin</code> and <code>end</code> blocks is not allowed
DFunction must use <code>Write-Host</code> instead of <code>Write-Output</code>
Step-by-Step Solution
Solution:
  1. Step 1: Check pipeline input handling

    The function declares ValueFromPipeline=$true but lacks a process block, which runs once per pipeline item.
  2. Step 2: Understand block roles

    begin runs once before pipeline input, end runs once after. Without process, pipeline items are ignored.
  3. Final Answer:

    Missing process block to handle pipeline input -> Option A
  4. Quick Check:

    process block needed for pipeline items [OK]
Quick Trick: process block handles each pipeline item [OK]
Common Mistakes:
  • Thinking begin or end handle pipeline items
  • Removing ValueFromPipeline attribute
  • Using Write-Host instead of Write-Output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes