需要在 Powershell 脚本中添加“等待”命令

需要在 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

因此,如果有人能帮助我在邮箱创建后添加某种等待命令,并等到用户邮箱创建后再让脚本禁用 ActiveSync 等,那将非常有帮助。我认为仅使用 -wait 开关不起作用。

答案1

使用Start-Sleep命令:

Start-Sleep -s 10

将暂停脚本 10 秒。

答案2

我必须处理我之前编写的 Exchange 脚本中的一些时间问题。具体来说,我需要修改新创建的通讯组的权限,但需要等到通讯组实际创建后才能尝试修改它。

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只是提供一个进度条。

答案3

您可以将其作为后台作业运行,然后等待该作业完成。如下所示:

$enablemb = 开始作业 { 启用邮箱 -Identity "$DName" -数据库 "$RData" }
等待作业 $enablemb
接收作业 $enablemb

答案4

为什么不是这样的:

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

我在初始测试后使用此类命令并附加一个命令,start-sleep因为如果没有它,do till 会消耗大量处理器周期。所以我的命令看起来更像这样:

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

如果测试要快速改变状态的时候不要担心start-sleep

相关内容