# Adding Managers to Distro Fails with Variables

**URL:** <https://forums.powershell.org/t/adding-managers-to-distro-fails-with-variables/20031>\
**Category:** PowerShell Help\
**Created:** [July 30, 2022, 6:49pm UTC](https://forums.powershell.org/t/adding-managers-to-distro-fails-with-variables/20031 "2022-07-30T18:49:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpm](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rpm/32/1336_2.png) [@rpm](https://forums.powershell.org/u/rpm)\
**Post date:** [July 30, 2022, 6:49pm UTC](https://forums.powershell.org/t/adding-managers-to-distro-fails-with-variables/20031/1 "2022-07-30T18:49:18Z")

</div>

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:

```auto
$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.

```auto
$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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 30, 2022, 6:54pm UTC](https://forums.powershell.org/t/adding-managers-to-distro-fails-with-variables/20031/2 "2022-07-30T18:54:25Z")

</div>

> [@rpm](#):
>
> This code fails to run and hangs at a blinky cursor forever.

What have you tried already to debug your code? 😉

---

<div class="post-metadata">

**Author:** ![rpm](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rpm/32/1336_2.png) [@rpm](https://forums.powershell.org/u/rpm)\
**Post date:** [July 30, 2022, 6:59pm UTC](https://forums.powershell.org/t/adding-managers-to-distro-fails-with-variables/20031/3 "2022-07-30T18:59:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 30, 2022, 7:06pm UTC](https://forums.powershell.org/t/adding-managers-to-distro-fails-with-variables/20031/4 "2022-07-30T19:06:15Z")

</div>

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:

```auto
$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.

---

<div class="post-metadata">

**Author:** ![rpm](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rpm/32/1336_2.png) [@rpm](https://forums.powershell.org/u/rpm)\
**Post date:** [August 1, 2022, 12:08pm UTC](https://forums.powershell.org/t/adding-managers-to-distro-fails-with-variables/20031/5 "2022-08-01T12:08:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 1, 2022, 12:24pm UTC](https://forums.powershell.org/t/adding-managers-to-distro-fails-with-variables/20031/6 "2022-08-01T12:24:27Z")

</div>

> [@rpm](#):
>
> After adding the type cast, it works as expected.

Cool. 👍🏽 … and thanks for sharing it. 🙂

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/adding-managers-to-distro-fails-with-variables/20031/7 "2024-05-16T20:17:37Z")

</div>


