-1

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

Help is appreciated.

Thanks in advance.

joebegborg07
  • 869
  • 5
  • 16
  • 24
  • 1
    It would be easier to assist if you posted the results from the Invoke-Webrequest query, $MachinesRequest. Or at least similar data. – jski Nov 03 '17 at 13:16

2 Answers2

4

Defining a variable with -or will only result in the variable containing True, and nothing else:

PS C:\> $EnvironmentList = "Environments-4" -or "Environments-5" -or "Environments-41" -or "Environments-61"
PS C:\> $EnvironmentList
True

To store all these values you need to define your lists as arrays:

$EnvironmentList = "Environments-4","Environments-5" #,...
$MachineList = "srv-a*","srv-b*","srv-c*" #,...

Additionally, -contains does not work with wildcards, you have to use -like to use those.

Example:

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){
            foreach ($Test in $MachineList) {
                if($Machine.Name -like $Test){
                        $Machine.Name               
                }
            }
        }
    }
}
Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
0

The closest thing to what you're asking that I've found in the past has been the "-in" parameter of Where-Object. However, I'm not sure if it works with wildcards. You will need to test/experiment to find out.

My actual example:

$entries | Where-Object -Property account -In $accounts.account

Where $entries contained the large list of accounts I wanted to filter, and $accounts contained the smaller list of accounts I wanted to filter by. This command returned only the entries in $entries which contained (the "account" attribute of) one of the accounts listed in $accounts.

I hope this makes sense. I didn't discover this on my own, but found the answer sometime ago via internet searches.

optic
  • 121
  • 1
  • 2
  • 11