0

我正在构建一个Powershell脚本来管理通讯组和联系人。Active Directory新的ADObjects

enter image description here

但是,当我创建一个新的distributiongroup

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

我要5到60秒之间等待的东西,直到我可以访问组(添加成员等)。

问题是:为什么我必须等待?解决这个问题的最好方法是什么(一次只需要65次社交或300秒......)?

+0

什么问题?如何等待? – ravikanth

+0

阅读:http://stackoverflow.com/questions/9917513/set-adgroup-cannot-find-group-just-created-in-same-script-powershell –

+0

@ravikanth我编辑了我的问题,非常感谢! – HW90

回答

0

我发现的东西,工作正常时,至今:

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"} 

} 
0

该问题可能是由于复制延迟造成的。例如,它正在等待新创建的组复制到另一个域控制器或全局编录。请注意,通用组在复制到全局编录之前无法修改。

我没有自己尝试,但我认为要解决这个问题,最简单的方法是更改​​流中的对象创建顺序。不要先创建通讯组,然后再创建通讯录,您应该首先创建通讯对象,然后创建通讯组。在创建通讯组时,您可以指定-Members并传入新创建的联系人对象。

如果New-DistributionGroup抱怨它也不能找到新创建的联系人对象,你可以尝试,接下来的事情就是指定域控制器通过-DomainController用做New-MailContactNew-DistributionGroup