Try/Catch -erroraction stop

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Stuart Stuart 1 week, 1 day ago.

  • Author
    Posts
  • #66552
    Profile photo of Stuart
    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
    Profile photo of Sam Boutros
    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
      Profile photo of Stuart
      Stuart
      Participant

      Perfect, thank you that worked. Good article too.

You must be logged in to reply to this topic.