Check if string is in list of strings

Question:

Context:
We are making an API to get a list of all VMs and the filter it, using if loops, to return only VMs with name starting only with the values in $MachineList.

The list of servers is split in 2:

  • set 1: srv-a-1, srv-a-2, srv-b-1, srv-b-2, srv-c-1, srv-c-2, etc.
  • set 2: tst-a-1, tst-a-2, tst-b-1, tst-b-2, tst-c-1, tst-c-2, etc.

This is the script:

We’re trying to return just the items which match the values in the machine list however this is returning the full list of machines (both srv* and tst*).

Answer:

First and foremost, $MachineList = "srv-a*" -or "srv-b*" -or ... won’t do what you apparently think it does. It’s a boolean expression that evaluates to $true, because PowerShell interprets non-empty strings as $true in a boolean context. If you need to define a list of values, define a list of values:


Also, the -contains operator does exact matches (meaning it checks if any of the values in the array is equal to the reference value). For wildcard matches you need a nested Where-Object filter

A better approach in this scenario would be a regular expression match, though, e.g.:

Source:

Check if string is in list of strings by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply