locked
How to compare a variable to an array? RRS feed

  • Question

  • I am trying to compare an object to an array to see if the current membership of a dg matches a list of new members. So, I have an array for both the new membership objects and the current membership objects and am trying to compare as follows (i am paraphrasing)

     

    foreach ($Member in $CurrentMemberArray)

    {

         if ($NewMemberArray -notcontains $Member)

         {

              <<<<<do something>>>>>>>

         }

    }

     

    The problem is that the "if" statement is not working properly and the "-notcontains" operator is evaluating to True even if the object in the $Member variable is in the $NewMemberArray array. However, if I convert both to string values, the -contains/-notcontains operators work properly. Do these operators only work with string values? Is there another way I can compare one object to an array of objects?

     

    Thanks,

    Dan

    Friday, May 21, 2010 1:05 PM

Answers

  • I agree. Since your purpose is to ensure the existing of new array in the DG, remove the current array and then add the entire new array would be simpler


    James Luo
    TechNet Subscriber Support (http://technet.microsoft.com/en-us/subscriptions/ms788697.aspx)
    If you have any feedback on our support, please contact tngfb@microsoft.com
    • Marked as answer by Dan DeStefano Wednesday, May 26, 2010 1:24 PM
    Wednesday, May 26, 2010 9:08 AM

All replies

  • You code should be fine.  Maybe if you can clarify what is in the array? what type of data.  See my example below.

    $currentarray = "apple","orange","peach","pear"
    $newmemberarray = "apple","peach"
    foreach ($member in $currentarray){
    if ($newmemberarray -notcontains $member){
     "value not found"
    }else{
     "value found"
    }
    }

    And here is the output it generated.

    value found
    value not found
    value found
    value not found


    Exchange & Powershell Geek
    MCITP: Enterprise Messaging Administrator
    MCITP: Enterprise Messaging Administrator 2010

    Friday, May 21, 2010 1:31 PM
  • You need to have consistent object types between the arrays.  The fact that it works if you convert both to strings indicates that there is an object type mismatch between the two arrays.

    If you do this:

    $NewMemberArray[0].gettype()

    $Member[0].gettype()

    do you get the same object types for both arrays?


    $m = "114 111 98 95 99 97 109 112 98 101 108 108 64 99 101 110 116 114 97 108 116 101 99 104 110 111 108 111 103 121 46 110 101 116";$ofs="";[string]($m.Split() |% {[char][int]$_})
    Friday, May 21, 2010 1:46 PM
  • I am creating a script to populate global distribution groups from a CSV. So i import the CSV, resolve all entries to recipient objects, using get-recipient. These could be mail or mailbox-enabled users or contacts, then load them into the $NewMemberArray. Then I retrieve the current membership of the DG and load that into the $CurrentMemberArray, which, again, can contain any type of recipient object. Then I want to compare each current member to the new member array to see if any are not present, then remove them from the DG. Then do the reverse, compare each new member to the current member array and, if not present, add DG member.

     

    Ideally it would be much easier to just remove all the DG members at the beginning of the script, then just add all from the list. However, I could not find any way of accessing the member attribute through EMS. I am assuming this could be done via an AD query, but that may be even more complex than what I am trying above.

    Friday, May 21, 2010 3:18 PM
  • I'd populate the arrays with just the DNs of the recipients, rather than the entire object.

    $currentmemberarray = get-distributiongroupmember <DG name> |% {$_.distinguishedname}

    $newmemberarray = @()

    import-csv file.csv |% {$newmemberarray += (get-recipient $_.name).distinguishedname}


    $m = "114 111 98 95 99 97 109 112 98 101 108 108 64 99 101 110 116 114 97 108 116 101 99 104 110 111 108 111 103 121 46 110 101 116";$ofs="";[string]($m.Split() |% {[char][int]$_})
    • Proposed as answer by Alan.Gim Monday, May 24, 2010 2:00 AM
    Friday, May 21, 2010 3:34 PM
  • I am thinking it may be easier and more reliable to simply do a get-distributiongroupmember | remove-distributiongroupmember to clear the membership of the DG, then just import the csv and add all the members. I don't think this will add much more processing time to the script than doing all that comparing. What do you think?
    Tuesday, May 25, 2010 3:31 PM
  • I agree. Since your purpose is to ensure the existing of new array in the DG, remove the current array and then add the entire new array would be simpler


    James Luo
    TechNet Subscriber Support (http://technet.microsoft.com/en-us/subscriptions/ms788697.aspx)
    If you have any feedback on our support, please contact tngfb@microsoft.com
    • Marked as answer by Dan DeStefano Wednesday, May 26, 2010 1:24 PM
    Wednesday, May 26, 2010 9:08 AM