wake-up-neo.com

需要在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开关是行不通的。

41
Paul Masek

使用 Start-Sleep命令:

Start-Sleep -s 10

将暂停脚本10秒钟。

95
Sven

我不得不在前一段时间编写的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只是提供进度条。

8
sysadmin1138

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

$ enablemb =开始作业{启用邮箱-身份“ $ DName”-数据库“ $ RData”}
Wait-Job $ enablemb
Receive-Job $ enablemb

4
Jason Berg

为什么不这样:

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

我将这种类型的命令与start-sleep进行初始测试后,因为这样做会吃掉没有它的许多处理器周期。所以我的看起来更像这样:

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

如果测试要快速更改状态,则不必担心start-sleep

1
Sean
1
Bret Fisher