Your PowerGUI Community: Simplify your PowerShell related GUI and script editing needs.


Can you build a delay in a powershell script?

This question is answered
My script works fine until I get to the last line where I enabled it as a distribution group in exchange. I get the error message that says:

The recipient "playlsi.com/test/testmail" could not be read from domain controller "dln1001p.playlsi.com". This may be due to replication delays. Switching out of Forest mode should allow this operation to complete successfully.
At :line:16 char:25
+ Enable-DistributionGroup  <<<< $name -PrimarySmtpAddress $email


If I do the last line 10 seconds later it works fine. I think if I delay the last line a few seconds the whole script will work flawlessly.

Here is my script:

$GroupName = Read-Host -Prompt 'Enter Group Name'
#OU where to look for OUs
$OU = "OU=test,DC=Playlsi,DC=com"
#Gets a list of all OUs inside $OU
get-qadobject -type OrganizationalUnit -sizeLimit 0 -searchRoot $ou |
# Exclude the parent container
where {$_.ParentContainerDN -eq $ou } |

foreach {
$name = $_.name -replace "_.*$","_" + $GroupName
$email = $name + "@playlsi.com"
New-QADGroup -ParentContainer $OU -Name $name  -DisplayName $name -samAccountName $name -GroupType Distribution -GroupScope Universal |
Set-QADGroup -email $email
#Mail enables so it appears in Exchange as a distribution group
Enable-DistributionGroup $name -PrimarySmtpAddress $email
}
Verified Answer
  • In PowerShell v1 you can add delay by doing:

    [System.Threading.Thread]::Sleep(10000)

    In PowerShell v2 there is a start-sleep cmdlet:

    Start-Sleep -Second 10
All Replies
  • In PowerShell v1 you can add delay by doing:

    [System.Threading.Thread]::Sleep(10000)

    In PowerShell v2 there is a start-sleep cmdlet:

    Start-Sleep -Second 10
  • Yup that worked perfectly.
    Thanks Dmitry!
  • Can you share the location you added "[System.Threading.Thread]::Sleep(10000)" to your script? I'm really new to scripting sorry for the newbe question.
  • This thread was already answered and closed. Please open a new thread and don't forget to include your code so the community has something to review for your question. Each script can have a myriad of goals, so providing your code helps us provide an accurate response.

    thx