Bird
0
0

You want to create an advanced function that accepts pipeline input by property name and supports ShouldProcess. Which of these parameter declarations is correct?

hard📝 Application Q8 of 15
PowerShell - Functions
You want to create an advanced function that accepts pipeline input by property name and supports ShouldProcess. Which of these parameter declarations is correct?
A[Parameter(ValueFromPipelineByPropertyName=$true)] [string]$ComputerName
B[Parameter(ValueFromPipeline=$true)] [int]$ComputerName
C[Parameter(ValueFromPipelineByPropertyName=$false)] [string]$ComputerName
D[Parameter(ValueFromPipelineByPropertyName=$true)] [int]$ComputerName
Step-by-Step Solution
Solution:
  1. Step 1: Understand pipeline input attributes

    ValueFromPipelineByPropertyName=$true allows the parameter to accept pipeline input by matching property names.
  2. Step 2: Check parameter type and attribute

    ComputerName is typically a string. [Parameter(ValueFromPipelineByPropertyName=$true)] [string]$ComputerName correctly uses string type and ValueFromPipelineByPropertyName=$true.
  3. Step 3: Evaluate other options

    [Parameter(ValueFromPipeline=$true)] [int]$ComputerName uses ValueFromPipeline=$true but type int is unlikely for ComputerName. [Parameter(ValueFromPipelineByPropertyName=$false)] [string]$ComputerName disables pipeline input. [Parameter(ValueFromPipelineByPropertyName=$true)] [int]$ComputerName uses int type incorrectly.
  4. Final Answer:

    [Parameter(ValueFromPipelineByPropertyName=$true)] [string]$ComputerName -> Option A
  5. Quick Check:

    Pipeline input by property name needs matching property and correct type [OK]
Quick Trick: Use ValueFromPipelineByPropertyName for property name pipeline input [OK]
Common Mistakes:
  • Using wrong parameter type for pipeline input
  • Confusing ValueFromPipeline and ValueFromPipelineByPropertyName
  • Disabling pipeline input accidentally

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes