none
Error Handling in Exchange Shell RRS feed

  • Question

  • Hello Everyone,

    I need a help for capturing the errors that occur while running any command in Exchange Shell.

    For Ex: I am having multiple users for which I want to get their mailbox details and also capture the ones which are not having any mailbox.

    Import-csv  .\ABC.csv | Foreach{Get-Mailbox $_.Alias}

    The operation couldn't be performed because object 'ABC' couldn't be found on
    'POCITMSLNTCDC01.LntCorp.LntUniverse.com'.
        + CategoryInfo          : NotSpecified: (:) [Get-Mailbox], ManagementObjectNotFoundException
        + FullyQualifiedErrorId : [Server=POCITMSXMB07,RequestId=55e3f83a-1440-47a4-9eba-ffee48d41a8a,TimeStamp=14-11-2016
        17:34:07] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] 6EBE134,Microsoft.Exchange.Management.Recipi
      entTasks.GetMailbox
        + PSComputerName        : pocitmsxmb07.lntuniverse.com

    I need to catch the above error and keep the loop running till all the rows in the input file is touched.

    Thanks,

    RON

    Monday, November 14, 2016 5:36 PM

Answers

  • You always find the last error in the automatic variable $Error.

    And you might take a look at error handling with try catch in Powershell

    Get-Help about_try_catch


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''



    Tuesday, November 15, 2016 9:59 AM
  • $Notfound = @()
    Import-Csv .\MailBoxs.csv | Foreach {
        If (!(Get-Mailbox -Identity $_.Alias -ErrorAction SilentlyContinue)) {
            $Notfound += $_.Alias
        }
    }

    Remove Write-Host if you don't need it

    $Notfoundss = @()
    Import-Csv .\MailBox.csv | Foreach {
        If (Get-Mailbox -Identity $_.Alias -ErrorAction SilentlyContinue) {
           Write-Host "Found $($_.Alias) Mailbox"
        }Else { 
           Write-Host "Cannot find $($_.Alias) Mailbox"
           $Notfoundss += $_.Alias
        }
    }

    Tuesday, November 15, 2016 11:38 AM

All replies

  • With the Get-Mailbox cmdlet, you should be able to add -ErrorAction SilentlyContinue to dismiss the error. Place it after your $_.Alias, but inside the curly brackets. Give that a go and see if it'll suit you.
    Monday, November 14, 2016 5:53 PM
  • Hi Tommy,

    I dont want to dismiss the error but I need to save them in some variable so that I will have the data that is processed & data that has not processed.

    Tuesday, November 15, 2016 9:38 AM
  • You always find the last error in the automatic variable $Error.

    And you might take a look at error handling with try catch in Powershell

    Get-Help about_try_catch


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''



    Tuesday, November 15, 2016 9:59 AM
  • $Notfound = @()
    Import-Csv .\MailBoxs.csv | Foreach {
        If (!(Get-Mailbox -Identity $_.Alias -ErrorAction SilentlyContinue)) {
            $Notfound += $_.Alias
        }
    }

    Remove Write-Host if you don't need it

    $Notfoundss = @()
    Import-Csv .\MailBox.csv | Foreach {
        If (Get-Mailbox -Identity $_.Alias -ErrorAction SilentlyContinue) {
           Write-Host "Found $($_.Alias) Mailbox"
        }Else { 
           Write-Host "Cannot find $($_.Alias) Mailbox"
           $Notfoundss += $_.Alias
        }
    }

    Tuesday, November 15, 2016 11:38 AM