How to compare a variable to an array?
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
May 21st, 2010 4:05pm

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
Free Windows Admin Tool Kit Click here and download it now
May 21st, 2010 4:31pm

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]$_})
May 21st, 2010 4:46pm

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.
Free Windows Admin Tool Kit Click here and download it now
May 21st, 2010 6:18pm

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]$_})
May 21st, 2010 6:34pm

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?
Free Windows Admin Tool Kit Click here and download it now
May 25th, 2010 6:31pm

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 simplerJames 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
May 26th, 2010 12:08pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics