none
Exchange CMDlets Bug -ErrorAction RRS feed

  • Question

  • So I love using my try/catch statements well nearly as much as the $? variable. Anyways turns out that I think the -erroraction switch is well... broken on some exchange CMDlets since Exchange 2013. Maybe it's been fixed in Exchange 2016 or Exchange 2019 well at least I hope so? 

    Although the global ErrorActionPreferance is set to continue we would expect the -erroraction to provide a terminating error but unfortunately not. If you run this on 2010 it works as expected   

          #$ErrorActionPreference = "STOP"
    
          $ErrorActionPreference = "Continue"
          $User = "UserThatDoesNotExisitAndShouldFail"
    
          Try{
    
            Write-Host "Running CMDlet: Get-Recipient" -ForegroundColor yellow
            Get-Recipient $User -ErrorAction Stop
        }
    
        Catch{
    
            Write-Host "Failed Get-Recipient $($_.Exception.Message)" -ForegroundColor Green
            
        }
    
          Try{
    
            Write-Host "Running CMDlet: Get-User" -ForegroundColor yellow
            Get-User $User -ErrorAction Stop
        }
    
        Catch{
    
            Write-Host "Failed Get-User $($_.Exception.Message)" -ForegroundColor Green
            
        }
    
          Try{
    
            Write-Host "Running CMDlet: Get-Mailbox" -ForegroundColor yellow
            Get-Mailbox $User -ErrorAction Stop
        }
    
        Catch{
    
            Write-Host "Failed Get-Mailbox $($_.Exception.Message)" -ForegroundColor Green
            
        }

    Test on Exchange 2013 CU21

    Running CMDlet: Get-Recipient

    The operation couldn't be performed because object 'UserThatDoesNotExisitAndShouldFail' couldn't be found on 'server'.

        + CategoryInfo          : NotSpecified: (:) [Get-Recipient], ManagementObjectNotFoundException

        + FullyQualifiedErrorId : [Server=server,RequestId=0be0,TimeStamp=7/2/2019 12:04:35 PM] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] BDC3F6BE,Microsoft.Exchange.Management.RecipientTasks.GetRecipient

    Running CMDlet: Get-User

    The operation couldn't be performed because object 'UserThatDoesNotExisitAndShouldFail' couldn't be found on ''.

        + CategoryInfo          : NotSpecified: (:) [Get-User], ManagementObjectNotFoundException

        + FullyQualifiedErrorId : [Server=,RequestId=cdab,TimeStamp=7/2/2019 12:04:36 PM] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] C3B3B546,Microsoft.Exchange.Management.RecipientTasks.GetUser

        + PSComputerName        : rtlbruexch09.rtlnet.be

    Running CMDlet: Get-Mailbox

    The operation couldn't be performed because object 'UserThatDoesNotExisitAndShouldFail' couldn't be found on ''.

        + CategoryInfo          : NotSpecified: (:) [Get-Mailbox], ManagementObjectNotFoundException

        + FullyQualifiedErrorId : [Server=,RequestId=4018097,TimeStamp=7/2/2019 12:04:36 PM] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] 7A009AC1,Microsoft.Exchange.Management.RecipientTasks.GetMailbox

    on the flip side forcing global VAR 

    $ErrorActionPreference = "STOP"
    
                  #$ErrorActionPreference = "Continue"
                  $User = "UserThatDoesNotExisitAndShouldFail"
    
                  Try{
                    
                        Write-Host "Running CMDlet: Get-Recipient" -ForegroundColor yellow
                        Get-Recipient $User -ErrorAction Stop
                    }
                    
                    Catch{
    
                        Write-Host "Failed Get-Recipient $($_.Exception.Message)" -ForegroundColor Green
                        
                    }
    
                  Try{
                    
                        Write-Host "Running CMDlet: Get-User" -ForegroundColor yellow
                        Get-User $User -ErrorAction Stop
                    }
                    
                    Catch{
    
                        Write-Host "Failed Get-User $($_.Exception.Message)" -ForegroundColor Green
                        
                    }
    
                  Try{
                    
                        Write-Host "Running CMDlet: Get-Mailbox" -ForegroundColor yellow
                        Get-Mailbox $User -ErrorAction Stop
                    }
                    
                    Catch{
    
                        Write-Host "Failed Get-Mailbox $($_.Exception.Message)" -ForegroundColor Green
                        
                    }

    Running CMDlet: Get-Recipient

    Failed Get-Recipient The operation couldn't be performed because object 'UserThatDoesNotExisitAndShouldFail' couldn't be found on 'server'.

    Running CMDlet: Get-User

    Failed Get-User The operation couldn't be performed because object 'UserThatDoesNotExisitAndShouldFail' couldn't be found on ''.

    Running CMDlet: Get-Mailbox

    Failed Get-Mailbox The operation couldn't be performed because object 'UserThatDoesNotExisitAndShouldFail' couldn't be found on ''.

    Am I missing something here or is this a bug that needs fixing? 



    • Edited by Josh Bines Tuesday, July 2, 2019 5:45 PM
    Tuesday, July 2, 2019 5:44 PM

All replies

  • Hi,

    The error information shows that when cmdlets are used to get information, "UserThatDoesNotExisitAndShouldFail" is recognized as the value of $User.

    We replace "UserThatDoesNotExisitAndShouldFail" with some specific user names. If we use a user name that doesn't exist, here is the result:

    We test with Exchange 2010 SP3 RU26, Exchange 2013 CU22 and Exchange 2016 CU12, the same result.

    You mentioned that "run this on 2010 it works as expected", what's the detailed version of your Exchange 2010?

    What did get from the script on your Exchange 2010? If it's convenient for you, you can provide the screenshot here, and please don't forget to cover your personal information.

    Regards,

    Lydia Zhou


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, July 3, 2019 5:25 AM
    Moderator
  • Thanks for the reply Lydia. 

    So we want it to fail with a terminating error and get caught by the CATCH statement. Which script did you test with in my post? it should be the first script the second will work it's important for the Erroractionpreferance to equal "Continue"

    Try this

          #$ErrorActionPreference = "STOP"
    
          $ErrorActionPreference = "Continue"
    
    Write-Host "ErrorActionPreference: $ErrorActionPreference"
    
          $User = "UserThatDoesNotExisitAndShouldFail"
    
          Try{
    
            Write-Host "Running CMDlet: Get-Recipient" -ForegroundColor yellow
            Get-Recipient $User -ErrorAction Stop
        }
    
        Catch{
    
            Write-Host "Failed Get-Recipient $($_.Exception.Message)" -ForegroundColor Green
            
        }
    
          Try{
    
            Write-Host "Running CMDlet: Get-User" -ForegroundColor yellow
            Get-User $User -ErrorAction Stop
        }
    
        Catch{
    
            Write-Host "Failed Get-User $($_.Exception.Message)" -ForegroundColor Green
            
        }
    
          Try{
    
            Write-Host "Running CMDlet: Get-Mailbox" -ForegroundColor yellow
            Get-Mailbox $User -ErrorAction Stop
        }
    
        Catch{
    
            Write-Host "Failed Get-Mailbox $($_.Exception.Message)" -ForegroundColor Green
            
        }

    More details here

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-6#erroraction  

    Thursday, July 4, 2019 12:23 PM
  • Hi, 

    Did you run the script by connecting to the Exchange server using remote PowerShell?

    If so, you should check this document and use this  three-step process to connect to Exchange servers: Connect to Exchange servers using remote PowerShell

    We test the script you provided in the latest reply, it works well:

    Regards,

    Lydia Zhou


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, July 5, 2019 8:13 AM
    Moderator
  • Hmm... that's not the result I was expecting. 

    I tested with remote connection in the powershell ISE but I also tested from the EMS with the same error. 

    Something werid is happening here. I'll do some more testing. Thank you for testing. 

    Monday, July 8, 2019 10:30 AM
  • Hi,

    We tested with EMS as well and we used Exchange 2013 CU22. The latest version of Exchange 2013 is Exchange 2013 CU23, you can try to upgrade to the latest version. Something may be fixed after the upgrade.

    Regards,

    Lydia Zhou


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, July 9, 2019 8:43 AM
    Moderator
  • If you have any updates or need further assistance, you are welcomed to post back.

    Hope your issue can be solved soon.

    Regards,

    Lydia Zhou


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, July 15, 2019 2:10 AM
    Moderator