Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string is in list of strings

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:

$EnvironmentList = "Environments-4" -or "Environments-5" -or "Environments-41" -or "Environments-61"
$MachineList = "srv-a*" -or "srv-b*" -or "srv-c*" -or "srv-d*" -or "srv-e*" -or "srv-f*" -or "srv-g*" -or "srv-h*" -or" srv-i*" -or "srv-j*" -or "srv-k*" -or "srv-l*"

function CheckService {
    $MachinesRequest = (Invoke-WebRequest -Method Get -Headers @{"X-system-ApiKey"="Hashed-API-Key-Value"} -URI https://url-to-site.local/api/machines/all).Content | ConvertFrom-Json
    foreach ($Machine in $MachinesRequest) {
        if ($EnvironmentList -contains $Machine.EnvironmentIds) {
            if ($MachineList -contains $Machine.Name) {
                $Machine.Name
            }
        }
    }
}

CheckService

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*).

like image 937
joebegborg07 Avatar asked Nov 03 '17 12:11

joebegborg07


People also ask

How do you check if a string is present in a list of strings?

Python Find String in List using count() We can also use count() function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list. l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C'] s = 'A' count = l1.

How do you check if a string value is in a list?

Using any() to check if string contains element from list. Using any function is the most classical way in which you can perform this task and also efficiently. This function checks for match in string with match of each element of list.

How do you check if a string is present in a list of strings in Java?

contains() in Java. ArrayList contains() method in Java is used for checking if the specified element exists in the given list or not. Returns: It returns true if the specified element is found in the list else it returns false.

How do you check if multiple strings are in a list?

Use the any() function to check if multiple strings exist in another string, e.g. if any(substring in my_str for substring in list_of_strings): . The any() function will return True if at least one of the multiple strings exists in the string.


1 Answers

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:

$MachineList = "srv-a*", "srv-b*", ...

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

$MachineList = "srv-a*", "srv-b*", "srv-c*", ...
...
if ($MachineList | Where-Object {$Machine.Name -like $_}) {
    ...
}

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

$pattern = '^srv-[a-l]'
...
if ($Machine.Name -match $pattern) {
    ...
}
like image 160
Ansgar Wiechers Avatar answered Sep 22 '22 10:09

Ansgar Wiechers