Bird
0
0

You want to create a function that accepts pipeline input and outputs the square of each number. Which of the following function definitions correctly implements this?

hard📝 Application Q8 of 15
PowerShell - Functions
You want to create a function that accepts pipeline input and outputs the square of each number. Which of the following function definitions correctly implements this?
Afunction Square-Number { param($Number) begin { Write-Output ($Number * $Number) } }
Bfunction Square-Number { param([Parameter(ValueFromPipeline=$true)]$Number) process { Write-Output ($Number * $Number) } }
Cfunction Square-Number { param([Parameter(ValueFromPipelineByPropertyName=$true)]$Number) process { Write-Output ($Number * $Number) } }
Dfunction Square-Number { param([Parameter(ValueFromPipeline=$false)]$Number) process { Write-Output ($Number * $Number) } }
Step-by-Step Solution
Solution:
  1. Step 1: Identify pipeline input by value

    function Square-Number { param([Parameter(ValueFromPipeline=$true)]$Number) process { Write-Output ($Number * $Number) } } uses ValueFromPipeline=$true, enabling pipeline input by value.
  2. Step 2: Check output logic

    The process block outputs the square of each input number correctly.
  3. Step 3: Analyze other options

    function Square-Number { param($Number) begin { Write-Output ($Number * $Number) } } lacks pipeline input attribute. function Square-Number { param([Parameter(ValueFromPipelineByPropertyName=$true)]$Number) process { Write-Output ($Number * $Number) } } uses pipeline input by property name, which requires objects with matching property names. function Square-Number { param([Parameter(ValueFromPipeline=$false)]$Number) process { Write-Output ($Number * $Number) } } disables pipeline input.
  4. Final Answer:

    function Square-Number { param([Parameter(ValueFromPipeline=$true)]$Number) process { Write-Output ($Number * $Number) } } -> Option B
  5. Quick Check:

    ValueFromPipeline=$true with process block = correct pipeline input [OK]
Quick Trick: Use ValueFromPipeline=$true and process block for pipeline input functions [OK]
Common Mistakes:
  • Using ValueFromPipelineByPropertyName when input is simple values
  • Placing logic in begin block instead of process
  • Setting ValueFromPipeline to $false

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes