How To Accept Multiple Values From Pipeline Into A Powershell Function
1 min readJan 20, 2022
The aim of this pageđź“ť is to demonstrate a tiny Select-String
(aka grep
) PowerShell function accepting multiple inputs from the pipeline in a loop that outputs only matches and not highlighted match of the whole string. I use this for getting exact keys from HashiCorp Consul, which I directly modify via a scripted solution, without a need to drill down to the key/value in the UI.
1. steps
- In
func1
, explicitly expects a list of strings as an input type[String[]]
- In
func2
, explicitly declare the parameter as an array of strings[String[]]
, not just a string withValueFromPipeline = $true
. - In
func2
, you will need to use thebegin{}
,process{}
,end{}
— even though some of those blocks are just declared/empty - IN
func2
, it does not work if you replace$match
with$_
to make it more readable
- regex support is built-in, don’t be shy to use it in lookups, see
ami_size.*
â–¶ query-consul org.acme_prod,com.acme_dev,net.acme_qa ami_size.* | print-matches-only
ami_size:large
ami_size:large
ami_size:large