Welcome to Powergui.org - an open source community for Windows Powershell

PowerGUI.org PowerGUI.org and blogs

Forums » Active Directory and PowerShell

Thread: Can you build a delay in a powershell script?

This question is answered.


Permlink Replies: 4 - Pages: 1 - Last Post: Apr 5, 2012 9:44 AM by: GregVM
Jon Reider

Posts: 49
Registered: 3/16/09
Can you build a delay in a powershell script?
Posted: Oct 15, 2009 8:24 AM
 
  Click to reply to this thread Reply

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
}



Dmitry Sotnikov


Posts: 1,241
Registered: 12/1/06
Re: Can you build a delay in a powershell script?
Posted: Oct 15, 2009 8:51 AM   in response to: Jon Reider
Answered
  Click to reply to this thread Reply

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


Jon Reider

Posts: 49
Registered: 3/16/09
Re: Can you build a delay in a powershell script?
Posted: Oct 15, 2009 9:09 AM   in response to: Dmitry Sotnikov
 
  Click to reply to this thread Reply

Yup that worked perfectly.
Thanks Dmitry!



johnswb

Posts: 1
Registered: 4/4/12
Re: Can you build a delay in a powershell script?
Posted: Apr 4, 2012 1:05 PM   in response to: Jon Reider
 
  Click to reply to this thread Reply

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.


GregVM


Posts: 732
Registered: 1/14/08
Re: Can you build a delay in a powershell script?
Posted: Apr 5, 2012 9:44 AM   in response to: johnswb
 
  Click to reply to this thread Reply

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

Greg Montoya
PowerGUI MVP
ActiveRoles Server MVP
Legend
MVP: 2501 + pts
Guru: 2001 - 2500 pts
Expert: 751 - 2000 pts
Enthusiast: 31 - 750 pts
Novice: 0 - 30 pts
Moderators
Helpful answer (5 pts)
Answered (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums