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

Pau*_*sek 52 email scripting powershell exchange exchange-2007

这是我当前的代码:

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 <John.Doe@xyz.com>" -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`nJohn.Doe@xyz.com" -SmtpServer exchange@xyz.com
Run Code Online (Sandbox Code Playgroud)

这段代码大约有一半的时间可以完美运行,但另一半我得到了这个错误的回报:

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               exchange@xyz.com      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
Run Code Online (Sandbox Code Playgroud)

因此,如果有人可以帮助我在邮箱创建后输入某种等待命令,并在脚本禁用 ActiveSync 等之前等到用户邮箱创建完毕,那将非常有帮助。我相信仅仅使用 -wait 开关是行不通的。

Sve*_*ven 118

使用Start-Sleep命令:

Start-Sleep -s 10
Run Code Online (Sandbox Code Playgroud)

将暂停脚本 10 秒钟。

  • 我不使用 Exchange,所以我对这方面的 cmdlet 不熟悉,但我要做的是尝试找到一个命令来检查资源是否存在,如果不存在,则进入一个保持周期,直到它存在。像这样: `while ( res-not-exist ) { Start-Sleep -s 1}` 这样,你只在必要时停止脚本,并且只在必要时停止。 (5认同)

sys*_*138 8

我不得不处理我不久前编写的 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)
Run Code Online (Sandbox Code Playgroud)

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