How To Accept Multiple Values From Pipeline Into A Powershell Function

Pavol Kutaj
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

  1. In func1, explicitly expects a list of strings as an input type [String[]]
  2. In func2, explicitly declare the parameter as an array of strings [String[]], not just a string with ValueFromPipeline = $true.
  3. In func2, you will need to use the begin{}, process{}, end{} — even though some of those blocks are just declared/empty
  4. 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

2. links

--

--

Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com