Need to add a "Wait" command to a Powershell script
Here is my current code:
Write-output “ENTER THE FOLLOWING DETAILS - When Creating Multiple New Accounts Go to EMC hit F5(refresh) and make sure previous new account is listed before proceeding to the next one”
$DName = Read-Host “User Diplay Name(New User)"
$RUser = Read-Host "Replicate User(Database Grab)"
$RData = ((Get-Mailbox -Identity $RUser).Database).DistinguishedName
$REmailInput = Read-Host “Requester's Name(Notification Email goes to this Person)"
$REmail = ((Get-Mailbox -Identity "$REmailInput").PrimarySmtpAddress).ToString()
Enable-Mailbox -Identity "$DName" -Database "$RData"
Set-CASMailbox -Identity "$DName" -ActiveSyncEnabled $false -ImapEnabled $false - PopEnabled $false
Send-MailMessage -From "John Doe <[email protected]>" -To $REmail -Subject "$DName's email account" -Body "$DName's email account has been setup.`n`n`nJohn Doe`nXYZ`nSystems Administrator`nOffice: 123.456.7890`[email protected]" -SmtpServer [email protected]
This code works flawlessly about half the time, but the other half I get this error in return:
ENTER THE FOLLOWING DETAILS - When Creating Multiple New Accounts Go to EMC hit
F5(refresh) and make sure previous new account is listed before proceeding to
the next one
User Diplay Name(New User): Jane Doe
Replicate User(Database Grab): Julie Doe
Requester's Name(Notification Email goes to this Person): Joanna Doe
Name Alias ServerName ProhibitSendQuo
ta
---- ----- ---------- ---------------
Jane Doe JDDAFA [email protected] unlimited
Set-CASMailbox : Jane Doe is not a mailbox user.
At C:\emailclientbasic.ps1:11 char:15
+ Set-CASMailbox <<<< -Identity "$DName" -ActiveSyncEnabled $false -ImapEnable
d $false -PopEnabled $false
+ CategoryInfo : NotSpecified: (0:Int32) [Set-CASMailbox], Manage
mentObjectNotFoundException
+ FullyQualifiedErrorId : 292DF1AC,Microsoft.Exchange.Management.Recipient
Tasks.SetCASMailbox
So if anyone could help me throw in some kind of wait command after the mailbox is created and wait until the user's mailbox is created before the script disables ActiveSync, etc it would be really helpful. I believe that simply using the -wait switch does not work.
Solution 1:
Use the Start-Sleep
command:
Start-Sleep -s 10
will pause the script for 10 seconds.
Solution 2:
I had to deal with some timing in an Exchange script I wrote a while ago. Specifically, I needed to modify permissions on a newly created distribution group, but needed to wait until the distribution-group was actually created before attempting to modify it.
do {
sleep -seconds 1
$mailboxExists = get-mailboxpermission -Identity "CN=$displayName,$DN" -User "NT AUTHORITY\SELF" -ErrorAction SilentlyContinue |fw IsValid
write-host "." -nonewline
} while (!$mailboxExists)
It just attempts to get the "IsValid" attribute off of the mailbox (in this example) as a proxy for "mailbox exists". Once get-mailboxpermission
returns true, the next step, setting a permission will actually function. The write-host
is just to provide a progress bar.
Solution 3:
You could run it as a background job and then wait for that job to finish. Like this:
$enablemb = Start-Job { Enable-Mailbox -Identity "$DName" -Database "$RData" }
Wait-Job $enablemb
Receive-Job $enablemb