2013-05-20 64 views
2

我有一個腳本,我用它來創建郵箱。我剛發現我的錯誤處理沒有按預期工作。

Write-Host "Attempting to create mailbox." 
[void](Enable-Mailbox johndoe ` 
    -DisplayName "Doe,John" ` 
    -Alias johndoe ` 
    -RetentionPolicy 'Custom Retention Policy' ` 
    -ActiveSyncMailboxPolicy 'Default') 
if($?){ 
    Write-Host "User & mailbox created"; Write-Host} 
else{ 
    Write-Host "Error creating user/mailbox" -ForegroundColor Red -BackgroundColor Yellow 
    exit} 
Add-MailboxPermission -Identity johndoe -User 'DOMAIN\ExchangeFullAdmins' -AccessRights 'FullAccess' 
if($?){ 
    Write-Host "Permissions set on mailbox"; Write-Host} 
else{ 
    Write-Host "Error setting ExchangeFullAdmins permissions on mailbox" -ForegroundColor Red -BackgroundColor Yellow 
exit} 

如果有錯誤創建郵箱,第一if($?)評估爲$true和第二個計算結果爲$false(因爲沒有郵箱作用於)。我期望兩者都可以評估爲$false。任何想法爲什麼第一個沒有正確地解僱?

這些是由每個指令產生的錯誤:

Enable-Mailbox : Unable to generate the e-mail address. Unable to load address module 'C:\Program Files\Microsoft\Excha 
nge Server\V14\Mailbox\address\SMTP\AMD64\inproxy.dll' for address type 'SMTP'. Additional message: 'Access is denied'. 
At \\server\share\temp.ps1:2 char:22 
+ [void](Enable-Mailbox <<<< johndoe ` 
+ CategoryInfo   : NotSpecified: (0:Int32) [Enable-Mailbox], RusException 
+ FullyQualifiedErrorId : 385E7D70,Microsoft.Exchange.Management.RecipientTasks.EnableMailbox 

Add-MailboxPermission : Cannot bind argument to parameter 'Identity' because it is null. 
At \\server\share\temp.ps1:13 char:33 
+  Add-MailboxPermission -Identity <<<< johndoe -User 'DOMAIN\ExchangeFullAdmins' -AccessRights 'FullAccess' 
    + CategoryInfo   : InvalidData: (:) [Add-MailboxPermission], ParameterBindingValidationException 
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Exchange.Management.RecipientTa 
    sks.AddMailboxPermission 

回答

2

我想是因爲類型轉換是成功的。刪除[void]並重試。

如果成功,那麼你可以代替[void]管道的嘗試Out-Null,看看是否得到一個錯誤:

Enable-Mailbox johndoe ` -DisplayName "Doe,John" ` -Alias johndoe ` -RetentionPolicy 'Custom Retention Policy' ` -ActiveSyncMailboxPolicy 'Default' | Out-Null

+0

那究竟是什麼。 在故障排除期間,我已經移除了'[void]',但是我沒有測試正確的情況組合,以確定它確實是問題所在。刪除它並使用'Out-Null'完全符合我的預期。 –