Answered by:
Foreach loop is returning same data multiple times instead of one

Question
-
Hi all,
I cannot figure it out why simple command in foreach loop is returning same data for each object passing it.
This is just example
$mailboxes = Get-Mailbox -ResultSize Unlimited | ? {$_.RecipientTypeDetails -eq 'UserMailbox' -or $_.RecipientTypeDetails -eq 'SharedMailbox'}
foreach ($m in $mailboxes) { do something}Does not matter if I use get or set or anything other I will get result of each object passing it. So if I have 100 mailboxes it will give me same result for every mailbox passing by inested of only one. Even if I do like this same result
foreach ($m in Get-Mailbox -ResultSize Unlimited | ? {$_.RecipientTypeDetails -eq 'UserMailbox' -or $_.RecipientTypeDetails -eq 'SharedMailbox'}) {do something}
- Edited by Zumberak Thursday, August 8, 2019 9:07 AM
Thursday, August 8, 2019 9:06 AM
Answers
-
Hi,
Try to run like this.
$mailboxes = (Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox,SharedMailbox).identity $mailboxes.foreach{ Set-Mailbox -Identity $_ -RoleAssignmentPolicy <policyname>}
-----------------------------------------------------------------------------------------------------------
If you found this post helpful, please give it a "Helpful" vote.
Please remember to mark the replies as answers if they help.
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.
- Marked as answer by Zumberak Friday, August 9, 2019 4:50 AM
Thursday, August 8, 2019 11:51 AM -
Hi,
of course it will behave that way...You need to edit the statement in the forach loop, so it looks like this:
$mailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox,SharedMailbox foreach ($m in $mailboxes) { Set-Mailbox -Identity $m.identity -RoleAssignmentPolicy <policyname> }
When you are in the foreach look each object is represented by the $m variable, which you specified in the condition. So in each loop, each mailbox is saved to $m. In order to get the Identity of it, while inside the loop, you need to reference it properly like in the example...
Please try it like this...
Regards,
(Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov
- Marked as answer by Zumberak Friday, August 9, 2019 4:50 AM
Thursday, August 8, 2019 12:19 PM
All replies
-
Hi,
this seems strange...I think it might have something to do with the data, saved in $mailboxes..
When you run:
$mailboxes = Get-Mailbox -ResultSize Unlimited | ? {$_.RecipientTypeDetails -eq 'UserMailbox' -or $_.RecipientTypeDetails -eq 'SharedMailbox'}
and then just check what is stored in $mailboxes..what do you see? Can you post a screenshot?
Can you please also test like this:
$mailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox,SharedMailbox
Regards,
(Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov
- Edited by Stoyan ChalakovMVP Thursday, August 8, 2019 9:26 AM
Thursday, August 8, 2019 9:24 AM -
Hi Stoyan,
Thank you for your answer. Both ways are giving the same result. When I run $mailboxes it gives me Name of the mailbox, alias, database , quota and externaldirectoryobjectid. I see all user and share mailboxes. Everything looks fine. But foreach loop is looping through all objects and returning the same result for each object so I get very long list of user mailboxes for each object.
- Edited by Zumberak Thursday, August 8, 2019 10:10 AM
Thursday, August 8, 2019 10:09 AM -
Hi,
can you post an example of what exactly are you doing in the {do something} section of your code?
Regards,
(Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov
Thursday, August 8, 2019 10:15 AM -
Would love to see an example of your for each aswell.
Given that the $mailboxes data looks fine (your own statement), my guess would be that you should look there for the cause of your issue.Thursday, August 8, 2019 10:52 AM -
Hi guys,
It's look like that it looping only when you run get command.
I am trying to set new roleassignmentpolicy on all mailboxes but when I run it is complaining on
Cannot bind argument to parameter 'Identity' because it is null.
+ CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Set-Mailbox
+ PSComputerName : outlook.office365.com
Here is the code.
$mailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox,SharedMailbox
foreach ($m in $mailboxes){Set-Mailbox -Identity $_.identity -RoleAssignmentPolicy <policyname> }
It should not be null because when I run $mailboxes.identity I get all users. I tried to run even like this $_ but same error message.
- Edited by Zumberak Thursday, August 8, 2019 11:45 AM
Thursday, August 8, 2019 11:44 AM -
Hi,
Try to run like this.
$mailboxes = (Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox,SharedMailbox).identity $mailboxes.foreach{ Set-Mailbox -Identity $_ -RoleAssignmentPolicy <policyname>}
-----------------------------------------------------------------------------------------------------------
If you found this post helpful, please give it a "Helpful" vote.
Please remember to mark the replies as answers if they help.
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.
- Marked as answer by Zumberak Friday, August 9, 2019 4:50 AM
Thursday, August 8, 2019 11:51 AM -
Hi,
of course it will behave that way...You need to edit the statement in the forach loop, so it looks like this:
$mailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox,SharedMailbox foreach ($m in $mailboxes) { Set-Mailbox -Identity $m.identity -RoleAssignmentPolicy <policyname> }
When you are in the foreach look each object is represented by the $m variable, which you specified in the condition. So in each loop, each mailbox is saved to $m. In order to get the Identity of it, while inside the loop, you need to reference it properly like in the example...
Please try it like this...
Regards,
(Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov
- Marked as answer by Zumberak Friday, August 9, 2019 4:50 AM
Thursday, August 8, 2019 12:19 PM