Email

需要向 Powershell 腳本添加“等待”命令

  • June 27, 2014

這是我目前的程式碼:

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

這段程式碼大約有一半的時間完美無缺,但另一半我得到了這個錯誤作為回報:

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

因此,如果有人可以在創建郵箱後幫助我輸入某種等待命令,並等到在腳本禁用 ActiveSync 之前創建使用者的郵箱等,那將非常有幫助。我相信簡單地使用 -wait 開關是行不通的。

使用Start-Sleep命令:

Start-Sleep -s 10

將暫停腳本 10 秒。

我不得不在我不久前編寫的 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只是提供一個進度條。

引用自:https://serverfault.com/questions/270568