locked
What operator can I use to return two values from a multi valued string? RRS feed

  • Question

  • Hi, I am trying to search for a user based on the two values returned from $_.Licenses.AccountSKUID

    But the -eq operator will only return the results for the first match and not the other.  

    get-MSOLUser -All | where {$_.isLicensed -eq "TRUE" -and $_.Licenses.AccountSKUID -eq "sku_id1" -and "sku_id4"}

    How can I get it to work? 

    Thanks 


    Thursday, May 28, 2020 12:27 PM

All replies

  • You cannot concatenate logical matches.  You must fully specify each one.

    To understand how to use logical operators please start by reading the following help topic.

    help  about_Logical_Operators


    \_(ツ)_/

    Thursday, May 28, 2020 3:10 PM
  • You cannot concatenate logical matches.  You must fully specify each one.

    To understand how to use logical operators please start by reading the following help topic.

    help  about_Logical_Operators


    \_(ツ)_/

     I did and that is why I was asking. 
    Thursday, May 28, 2020 3:14 PM
  • You didn't read it carefully.  Look at the examples and then look at what you have written.


    \_(ツ)_/

    Thursday, May 28, 2020 3:20 PM
  • -eq works a little differently with arrays on the left side, but this would work:


    get-MSOLUser -All | where {$_.isLicensed -eq "TRUE" -and
    $_.Licenses.AccountSKUID -eq "sku_id1" -and
    $_.Licenses.AccountSKUID -eq "sku_id4"}

    Thursday, May 28, 2020 4:51 PM
  • Reconsider how this is stated.  You cannot correctly match a Boolean with a string.  It won't work correctly without careful structure.  Use Booleans correctly and this never happens.

    where {$_.isLicensed -and $_.Licenses.AccountSKUID -eq 'sku_id1' -and $_.Licenses.AccountSKUID -eq 'sku_id4'}

    A better way to structure long experssions for readability is as follows:

    where{
        $_.isLicensed -and 
        $_.Licenses.AccountSKUID -eq 'sku_id1' -and
        $_.Licenses.AccountSKUID -eq 'sku_id4'
    }

    Of course and-ing two things this way never works as a string cannot be equal to two different strings.  I suspect you want OR.

    where{
        $_.isLicensed -and (
            $_.Licenses.AccountSKUID -eq 'sku_id1' -or
            $_.Licenses.AccountSKUID -eq 'sku_id4'
        )
    }

    Using "-match" can simplify this kind of statement.

    where{
        $_.isLicensed -and 
        $_.Licenses.AccountSKUID -match '^sku_id1$|^sku_id4$'
    }

    The ^$ force an exact match.

    The reason I recommended reading the documentation until it is fully understood is so the OP could see how to use these expressions and might actually do some research on how programmed logic expressions work and are used.


    \_(ツ)_/




    • Edited by jrv Thursday, May 28, 2020 5:42 PM
    Thursday, May 28, 2020 5:40 PM