Try/Catch -erroraction stop

This topic contains 4 replies, has 3 voices, and was last updated by  Rob Simmers 5 months, 2 weeks ago.

  • Author
    Posts
  • #66552

    Stuart
    Participant

    Hi Guys,

     try{
        New-MailboxImportRequest -Name $UPN -BatchName $BatchName -Mailbox $UPN -AzureBlobStorageAccountUri $CompleteURI -BadItemLimit 30 -LargeItemLimit 10 -AzureSharedAccessSignatureToken $AzureToken -TargetRootFolder $AzureRootFolder -ErrorAction Stop
        } #close try
        catch
        {
        "[ERROR]`t $($_.name), something went wrong $($_.Exception.message)" | Out-File $Azureimport -Append
        } #close catch
    

    Not sure why but when this errors the catch doesnt work. It just errors on the screen and not in the log. I realise its a non terinating error. The error in this case was:

    The operation couldn't be performed because '' matches multiple entries.
        + CategoryInfo          : NotSpecified: (:) [New-MailboxImportRequest], ManagementObjectAmbiguousException
        + FullyQualifiedErrorId : [Server=MMXP123MB0655,RequestId=7fac98cc-f438-460a-8bd0-9f7717af90f5,TimeStamp=16/03/2017 05:58:39] [FailureCategory=Cmdlet-ManagementObjectAmbiguousException] A1D35C99,Microsoft.Exchange.M 
       anagement.Migration.MailboxReplication.MailboxImportRequest.NewMailboxImportRequest
        + PSComputerName        : ps.outlook.com
    
    

    Any ideas welcomed

  • #66591

    Sam Boutros
    Participant

    Stuart,

    '-ErrorAction Stop' turns an error into a terminating error. See this blog post for more details.

    The $Error (System.Management.Automation.ErrorRecord) does not have a 'name' property. You may be looking for $_.CategoryInfo.TargetName

    $Azureimport = '.\Azureimport.txt'
    try {
        Get-ChildItem sam -ErrorAction Stop
    } catch {
        "[ERROR] '$($_.CategoryInfo.TargetName)' Something went wrong: '$($_.Exception.message)'" | 
            Out-File $Azureimport -Append
    } 
    notepad $Azureimport
    
    • #66619

      Stuart
      Participant

      Perfect, thank you that worked. Good article too.

  • #70201

    Stuart
    Participant

    Hello,

    The method above worked, but if the error is different, then do I need multiple catch statements?

    try{
        New-365MailboxImportRequest -Name $UPN -BatchName $BatchName -Mailbox $UPN -AzureBlobStorageAccountUri $CompleteURI -BadItemLimit 30 -LargeItemLimit 10 -AzureSharedAccessSignatureToken $AzureToken -TargetRootFolder $AzureRootFolder -ErrorAction Stop
        } #close try
        catch
        {
        "[ERROR] '$($_.CategoryInfo.TargetName)' Something went wrong: '$($_.Exception.message)'" | Out-File $Azureimport -Append
    
        } #close catch
    

    Get this error:

    Unable to open PST file 'https://.blob.core.windows.net/ingestiondata/hhs.stourport.sales.pst'. Error details: The remote server returned an error: (403) Forbidden.
        + CategoryInfo          : NotSpecified: (:) [New-MailboxImportRequest], RemotePermanentException
        + FullyQualifiedErrorId : [Server=MMXP12301MB1551,RequestId=491488ab-123e-4611-ac16-f8e78b68ba45,TimeStamp=05/05/2017 08:42:36] [FailureCategory=Cmdlet-RemotePermanentException] 3EC0D361,Microsoft.Exchange.Managemen 
       t.Migration.MailboxReplication.MailboxImportRequest.NewMailboxImportRequest
        + PSComputerName        : ps.outlook.com
    
    

    But becasue the stop action wasnt triggered, it moves onto this line

        "[INFO]`t Mailbox $UPN has been successfully queued for import" | Out-File $Azureimport -Append

    So in the log it looks like all is well, but in reality, the import failed. Any pointers on how to catch this error and any other errors that might be different?

    Many THanks

  • #70218

    Rob Simmers
    Participant

    In the catch block, an exception occurred and you need to decide what to do with the exception. Currently, you are only only writing a log file, but you are not terminating the execution of code. You need to use Throw or Write-Error to re-throw the error or nest the try statement:

    try {
        #Splatting: https://technet.microsoft.com/en-us/library/gg675931.aspx
        $params = @{
            Name = $UPN 
            BatchName = $BatchName 
            Mailbox = $UPN 
            AzureBlobStorageAccountUri = $CompleteURI 
            BadItemLimit = 30 
            LargeItemLimit = 10 
            AzureSharedAccessSignatureToken = $AzureToken 
            TargetRootFolder = $AzureRootFolder 
            ErrorAction = "Stop"
        }
        
        
        New365MailboxImportRequest @params
    } #close try
    catch {
        $msg = "[ERROR] '$($_.CategoryInfo.TargetName)' Something went wrong: '$($_.Exception.message)'" 
        $msg | OutFile $Azureimport Append
        #Stop code execution
        Throw $msg
        
    } #close catch
    

    or

    try {
        #Splatting: https://technet.microsoft.com/en-us/library/gg675931.aspx
        $params = @{
            Name = $UPN 
            BatchName = $BatchName 
            Mailbox = $UPN 
            AzureBlobStorageAccountUri = $CompleteURI 
            BadItemLimit = 30 
            LargeItemLimit = 10 
            AzureSharedAccessSignatureToken = $AzureToken 
            TargetRootFolder = $AzureRootFolder 
            ErrorAction = "Stop"
        }
        
        
        New365MailboxImportRequest @params
    
        #Only executed if previous command did not have exception
        try {
            Set-365Mailbox -Param Foo -ErrorAction Stop
        }
        catch {
            $msg = "[ERROR] '$($_.CategoryInfo.TargetName)' Next Command failed: '$($_.Exception.message)'" 
            $msg | OutFile $Azureimport Append
        }
    } #close try
    catch {
        $msg = "[ERROR] '$($_.CategoryInfo.TargetName)' Something went wrong: '$($_.Exception.message)'" 
        $msg | OutFile $Azureimport Append
        #Stop code execution
        #Throw $msg
        
    } #close catch
    

    There is a free eBook in the menu above for Powershell Error Handling

You must be logged in to reply to this topic.