需要添加一个“等待”命令到Powershell脚本

这是我现在的代码:

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] 

这段代码大概有一半的时间是完美无缺的,但是另一半我得到这个错误:

 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 

所以,如果有人能够帮助我在创build邮箱之后抛出某种等待命令,并且等到用户的邮箱被创build之后脚本禁用了ActiveSync等等,那将是非常有帮助的。 我相信只是使用-wait开关不起作用。

使用Start-Sleep命令:

 Start-Sleep -s 10 

将暂停脚本10秒。

我不得不在一个我刚刚写的Exchange脚本中处理一些时间。 具体而言,我需要修改新创build的通讯组的权限,但需要等到发行组实际创build之后再尝试对其进行修改。

 do { sleep -seconds 1 $mailboxExists = get-mailboxpermission -Identity "CN=$displayName,$DN" -User "NT AUTHORITY\SELF" -ErrorAction SilentlyContinue |fw IsValid write-host "." -nonewline } while (!$mailboxExists) 

它只是尝试从邮箱中获取“IsValid”属性(在本例中)作为“存在邮箱”的代理。 一旦get-mailboxpermission返回true,下一步,设置权限将实际上起作用。 write-host只是提供一个进度条。

您可以将其作为后台作业运行,然后等待该作业完成。 喜欢这个:

$ enablemb = Start-Job {启用邮箱-Identity“$ DName”-Database“$ RData”}
Wait-Job $ enablemb
Receive-Job $ enablemb

为什么不是这样的:

 do { $testpath = Test-Path -path \\dns2\d$\test } until ($testpath -eq $true) 

在初始testing之后,我使用这种types的命令进行了附加的start-sleep ,因为直到吃了很多没有它的处理器周期。 所以我看起来更像这样:

 do { $testpath = Test-Path -path \\dns2\d$\test start-sleep -s 10} until ($testpath -eq $true) 

如果testing快要改变状态,不要担心start-sleep