ForEach variable is $null in 2010, but not 2007

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 8 months ago.

  • Author
    Posts
  • #6494

    by mitchellguthrie at 2012-11-08 10:49:00

    Hi All,

    I have a PS script that's working well in Exchange 2007, but doesn't in Exchange 2010. In 2010 I get the error that $Recipients.DistinguishedName is null. Anyone point me in the right direction to get this working?

    What the script does is checks all the mailbox folders of the Linked Mailboxes and if it finds the folder "Cloud Archive" in their mailbox it will add them to the Distribution List "ArchiveCloudDistro"; works in a 2007 env, but not in 2010.

    $Recipients = Get-Recipient -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -ResultSize Unlimited -Filter {(RecipientTypeDetails -eq 'LinkedMailbox')}
    $Recipients | ForEach {(Get-MailboxFolderStatistics -Identity $_.DistinguishedName)} | Where {($_.FolderPath -eq "/Cloud Archive")} | ForEach {Add-DistributionGroupMember -Member $Recipients.DistinguishedName -Identity ArchiveCloudDistro}

    by DonJ at 2012-11-08 11:08:24

    Yeah, you can't actually do that.

    $Recipients is a collection of objects, and it doesn't have a DistinguishedName property. While PowerShell v3 will let you get away with that, v2 won't. And, keep in mind that what you're passing to -Member is every single recipient, not a filtered list.

    Follow what you're doing in the pipeline:

    $Recipients | < – puts all your recipients into the pipeline
    ForEach {(Get-MailboxFolderStatistics -Identity $_.DistinguishedName)} | < – puts mailbox folder stats into the pipeline
    Where {($_.FolderPath -eq "/Cloud Archive")} | < – filters the mailbox folder stat objects
    ForEach {Add-DistributionGroupMember -Member $Recipients.DistinguishedName -Identity ArchiveCloudDistro}

    That last ForEach is getting Mailbox Folder Statistic objects. You haven't done a thing to your $Recipients variable – it still contains every recipient you originally retrieved. So regardless of the filtering, even if you did this on PowerShell v3 you'd be adding every recipient to ArchiveCloudDistro.

    I think you may need to re-think your logic on the approach.

    by mitchellguthrie at 2012-11-21 08:22:34

    Thanks for the reply Don. I went back to the drawing board and came up with this which works perfectly!

    Get-Recipient -ResultSize Unlimited -Filter {(RecipientTypeDetails -eq 'LinkedMailbox')} | foreach{

    $mbx = Get-Mailbox $_.DistinguishedName | select DistinguishedName

    Get-MailboxFolderStatistics $_.DistinguishedName |Where {($_.FolderPath -eq "/Cloud Archive")} | foreach{
    $mbx | add-member -type noteProperty -name Identity -value $_.Identity
    }
    $mbx | Select DistinguishedName,Identity | Where {$_.Identity -ne $null} | ForEach {Add-DistributionGroupMember -Identity ArchiveCloudDistro -Member $_.DistinguishedName}
    }

You must be logged in to reply to this topic.