0

I'm building a Powershell script to manage distribution groups and contacts.

enter image description here

But when I create a new distributiongroup

function GroupAddGroup([string] $GroupName)
{
    $newGroup = New-DistributionGroup $GroupName;
    return $newGroup.Name
}

I have to wait something between 5 and 60 seconds until I can access the group (adding members and so on).

The question is: why do I have to wait? And what is the best way to solve it (perhabs one time it needs 65 soconds or 300 seconds....)?

HW90
  • 1,953
  • 2
  • 21
  • 45

2 Answers2

0

The problem is probably due to the replication delay. For example, it's waiting for newly created group to replicated to another domain controller or global catalog. Note that Universal Groups cannot be modified until it's replicated to Global Catalog.

I didn't try it myself but I think to solve the problem, the easiest way is to change the object creation sequence in your flow. Instead of creating the distribution group first and then the contacts, you should create the contacts object first and then create the distribution group. At the time you create the distribution group, you specify the -Members and pass in the newly created contact objects.

If New-DistributionGroup complains it cannot find the newly created contact objects either, the next thing you can try is to specifying the domain controller to use by -DomainController when doing the New-MailContact and New-DistributionGroup

Harvey Kwok
  • 11,713
  • 6
  • 37
  • 59
0

I found something that is working fine until now:

function DistributionGroupExists([string] $Identity)
{
    $timer = [diagnostics.stopwatch]::startnew()
    while ($timer.elapsed.totalseconds -lt 30){

    if (Get-DistributionGroup $Identity){
        break
    }
    else 
    {
    start-sleep -seconds 5
    }
    }

    $timer.stop()
    if (!(Get-ADObject $Identity)){Write-host "Warning: Mailbox creation failed for $user after $($timer.elapsed.totalseconds) seconds."}
    else {write-host "Mailbox creation successful for $user in $($timer.elapsed.totalseconds) seconds"}

}
HW90
  • 1,953
  • 2
  • 21
  • 45