0

I have to check for multiple array entries in an if statement.

if (($Right.IdentityReference -eq $User) -or ($Right.IdentityReference -eq ("Domain\" + $GroupArrayList[0])) -or ($Right.IdentityReference -eq ("Domain\" + $GroupArrayList[1])))

This will continue with $GroupArrayList[2], $GroupArrayList[3], ...

Is there any way how I can go trough every entry of the array? I can't write every position down because the array size is dynamic. How can I create such a loop?

1
  • $array | Where-Object { 'foo', 'bar', 'baz' -contains $_.IdentityReference } Commented Apr 24, 2019 at 12:52

3 Answers 3

1

You can use a Foreach

Foreach ($ArrayItem in $GroupArrayList) {
    if (($Right.IdentityReference -eq $User) -or ($Right.IdentityReference -eq ("Domain\" + $ArrayItem))) {
        # Do stuff
    }
}

The variable $ArrayItem will refer to your $GroupArrayList[2], $GroupArrayList[3],...

Sign up to request clarification or add additional context in comments.

Comments

1

I don't think you even need a loop for that, but instead use the -contains operator like this:

if (($Right.IdentityReference -eq $User) -or ($GroupArrayList -contains ($Right.IdentityReference -replace '^Domain\\',''))

You simply strip off the Domain\ from the $Right.IdentityReference and see if the string that remains can be found in the $GroupArrayList array.

Comments

1

As you're ORing the comparison's why not testing if -in array?

if ($Right.IdentityReference -in 
        $User,
        ("Domain\" + $GroupArrayList[0]),
        ("Domain\" + $GroupArrayList[1]) ) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.