You will have explored how the PowerShell pipeline works and the way built-in cmdlets cross knowledge from one command to a different. However do you know you’ll be able to create your individual customized features that additionally use the pipeline? Sure! With a parameter binding, your features can act similar to built-in cmdlets, seamlessly accepting knowledge from the pipeline.
This tutorial will information you to equipping your customized features with the facility of the pipeline, remodeling the way you automate and construction your PowerShell scripts.
Reasonably than counting on loops or handbook inputs, unlock new ranges of effectivity and suppleness in your scripting!
Enabling Pipeline Enter with ValueFromPipeline
On your perform to obtain data from the pipeline, you need to configure a number of parameters to just accept it. You’ll configure parameters to make use of the ValueFromPipeline
or ValueFromPipelineByPropertyName
attribute.
Let’s begin by organising a perform that installs software program on a number of computer systems to cross an array of pc names to it with out utilizing a loop.
Add the ValueFromPipeline
attribute to the ComputerName
parameter, permitting every incoming pipeline object to be handled because the ComputerName
parameter.
PowerShell will bind every incoming pipeline object to $ComputerName
with this setup.
perform Set up-Software program { param( [Parameter(Mandatory)] [ValidateSet(1,2)] [int]$Model, [Parameter(Mandatory, ValueFromPipeline)] [string]$ComputerName ) course of { Write-Host "I put in software program model $Model on $ComputerName. Yippee!" } }
Now, cross an array of pc names to the perform utilizing the pipeline.
$computer systems = @("SRV1", "SRV2", "SRV3") $computer systems | Set up-Software program -Model 1
With out the course of
block, the perform would solely deal with the final merchandise. The course of
block permits the perform to independently course of every merchandise within the array.
Binding by Property Title with ValueFromPipelineByPropertyName
Maybe you’ve gotten a number of properties in your pipeline objects that have to match the parameters of your perform. In that case, let’s assume a CSV file comprises each the ComputerName
and Model
fields.
To display binding by property title, begin by saving a CSV file containing these properties:
@( [pscustomobject]@{'ComputerName' = 'SRV1'; 'Model' = 1} [pscustomobject]@{'ComputerName' = 'SRV2'; 'Model' = 2} [pscustomobject]@{'ComputerName' = 'SRV3'; 'Model' = 2} ) | Export-Csv -Path C:Scriptssoftware_installs.csv -NoTypeInformation
Replace the perform to just accept Model
and ComputerName
by property title.
With ValueFromPipelineByPropertyName
, the perform matches properties in every object (corresponding to these imported from a CSV) with parameters within the perform.
perform Set up-Software program { param( [Parameter(Mandatory, ValueFromPipelineByPropertyName)] [ValidateSet(1,2)] [int]$Model, [Parameter(Mandatory, ValueFromPipelineByPropertyName)] [string]$ComputerName ) course of { Write-Host "I put in software program model $Model on $ComputerName. Yippee!" } }
Now, use Import-Csv
to import the file and pipe every object to Set up-Software program
.
Import-Csv -Path C:Scriptssoftware_installs.csv | Set up-Software program
Every row’s ComputerName
and Model
fields bind to the respective parameters within the perform. This strategy allows straightforward bulk processing of software program installations throughout a number of computer systems with model management.
Conclusion
On this tutorial, you discovered learn how to allow pipeline assist in PowerShell features, making them extra versatile and highly effective. Now you can design customized features that seamlessly settle for knowledge from the pipeline, much like built-in cmdlets.
With pipeline-enabled features, you acquire finer management over knowledge enter, simplifying script design and enhancing readability.
As you proceed working with PowerShell, think about integrating these methods to jot down environment friendly, pipeline-ready features. Make your scripts extra adaptable and efficient in automating duties!