Adding Managers to Distro Fails with Variables

Hello Everyone,
I’m stuck as to why this script works if I work through it with all hard-coded text but stalls and does not complete when I use variables.

This code works just fine:

$ManagedBy = (Get-DistributionGroup "accidentinvestigation@deltarfasteners.com").ManagedBy
$NewManagedBy = ($ManagedBy + "0247851","0158694","0238574","0236806","0206854")
$NewManagedBySorted = $NewManagedBy | Sort-Object -Unique
Set-DistributionGroup -Identity "accidentinvestigation@deltarfasteners.com" -ManagedBy $NewManagedBySorted

This code fails to run and hangs at a blinky cursor forever.

$mailboxes = Get-Content "C:\Users\rmartin\OneDrive - Illinois Tool Works, Inc\Scripts\content.txt" -First 1
$users = @("0247851","0158694","0238574","0236806","0206854")

ForEach ($mailbox in $mailboxes) {
    $ManagedBy = (Get-DistributionGroup $Mailbox).ManagedBy
    $NewManagedBy = ($ManagedBy + $Users)
    $NewManagedBySorted = $NewManagedBy | Sort-Object -Unique
	Set-DistributionGroup -Identity $mailbox -ManagedBy $NewManagedBySorted
    }

I’m running on the newest version of EXO, and it just doesn’t want to go. Any help is appreciated!

Thanks,
Rob

What have you tried already to debug your code? :wink:

So I wrote the ForEach first, and it hung, but in the other code block, where I have everything explicitly written out, it works; that’s the extent of my troubleshooting so far.

I’m unsure if I’m correctly passing the variable to the Set-Distribution Group for the identity.

Looking at the documentation from Microsoft, I shouldn’t even have to do the whole $NewManagedBySorted dance; I should be able to say:
Set-DistributionGroup -Identity $Something -ManagedBy @{Add=$Users} but that fails as well. It just hangs, no error code; I can’t even ctrl-c out of the command.

Hmmm … you told your code hangs with a blinky cursor. I’d try to figure out what command exactly is blocking. So I’d output all variables directly after I set them. Set the $VerbosePreference to Continue and run this:

$mailboxes = Get-Content "C:\Users\rmartin\OneDrive - Illinois Tool Works, Inc\Scripts\content.txt" -First 1
$users = @("0247851", "0158694", "0238574", "0236806", "0206854")

ForEach ($mailbox in $mailboxes) {
    $ManagedBy = (Get-DistributionGroup $Mailbox).ManagedBy
    Write-Verbose "ManagedBy: '$($ManagedBy)'"
    $NewManagedBy = ($ManagedBy + $Users)
    Write-Verbose "NeManagedBy: '$($NewManagedBy)'"
    $NewManagedBySorted = $NewManagedBy | Sort-Object -Unique
    Write-Verbose "NeManagedBySorted: '$($NewManagedBySorted)'"
    Set-DistributionGroup -Identity $mailbox -ManagedBy $NewManagedBySorted 
}

You should at least see where it stops working.

I emailed the EXO team at Microsoft, and they told me there’s a known bug.

Hi Martin,

We recently identified a bug in this particular scenario and are working on the fix. The fix should be >available in a couple of weeks. There is a workaround to read content from a file still and pass the >values as Identity by typecasting the object returned by Get-Content to a string array, as shown below –

[string[ ] ] $mailboxes = Get-Content “”

Please let us know if this does not work for you, and we can help find a way to work around this >issue.

Thanks & Regards
Pallavi

After adding the type cast, it works as expected.

1 Like

Cool. :+1:t4: … and thanks for sharing it. :slightly_smiling_face: