Issue with try/catch and nested if/else

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 1 year, 10 months ago.

  • Author
    Posts
  • #27264
    Profile photo of David Schmidtberger
    David Schmidtberger
    Participant

    I have error action preferences set to stop
    if the user is set for litigationhold, the code works, and I get the output exactly as intended.

    if the user has no mailbox, I see the powershell error from exchange indicating no mailbox (I would expect the try/catch to intercept this), and I then see the error message write-host.

    if the user has a mailbox, but is not on legal-hold, I only get the write-host error message from the catch, I would expect that to flow through to the if/else statement within the try/catch.

    any suggestions to make this work would be appreciated

    try
    {
    $LegalHoldStatus =get-mailbox $samaccountname  |select litigationholdenabled
    if ($LegalHoldStatus -like "*True*")
    {
    Write-Host "$samaccountname set for legalhold"
    }
    else
    {
    $LegalHoldStatus = " "
    Write-Host "$samaccountname not set for legalhold"
    }
    }
    catch
    {
    $LegalHoldStatus = " "
    Write-Host "error finding mailbox for litigationhold for $samaccountname ignore if this is a secondary account" -foregroundcolor "Red"
    }
    
  • #27265
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Where are you setting the preference? It's a better practice to handle error actions for the command itself:

    $LegalHoldStatus =get-mailbox $samaccountname -ErrorAction Stop | select litigationholdenabled
    

    If you update the code with the above, do you see the expected behavior?

  • #27266
    Profile photo of David Schmidtberger
    David Schmidtberger
    Participant

    this is just a snippet from a larger script.

    I set the preference before any logic portions any other variables.

    I can try this option, won't have feedback for day or so.

    what would the difference be between setting the preference globally vs on the individual commands?

  • #27268
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    That's why I'm asking where you set the preference. If it's set globally as the default, then the command should use that preference. If specifying on the command works, then there is most likely a scope issue and the command is still using the default "Continue" preference.

  • #27270
    Profile photo of Martin Nielsen
    Martin Nielsen
    Participant

    This wouldn't happen to be Exchange 2010 would it?

    In my experience Exchange 2010 cmdlets sometimes completely disregard erroraction when using try/catch unless you put them inside a function. Then for some reason they work just fine. I have yet to find the reason why...

  • #27272
    Profile photo of David Schmidtberger
    David Schmidtberger
    Participant

    I'm in O365, my memory is it is using the 2010 cmdlets

  • #27278
    Profile photo of David Schmidtberger
    David Schmidtberger
    Participant

    just wrote a fast test using the erroraction stop in the command, I am getting slighty different behavior, now the try/catch isn't functioning at all, it never winds up on the error finding section, if the mailbox doesn't exist, we run through the if/else statement, the try/catch doesn't seem to function at all come into it

  • #27297
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Some Get cmdlets return an exception if something is not found, others return NULL. For instance, Get-ADUser returns NULL and Get-Service provides an exception:

    PS C:\Windows\System32\WindowsPowerShell\v1.0> Get-ADUser -Filter "SamAccountName -eq 'MyBoyBlue'"
    
    PS C:\Windows\System32\WindowsPowerShell\v1.0> Get-Service -Name MyBoyBlue
    Get-Service : Cannot find any service with service name 'MyBoyBlue'.
    At line:1 char:1
    + Get-Service -Name MyBoyBlue
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (MyBoyBlue:String) [Get-Service], ServiceCommandException
        + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
     

    All that you can do is test your commands during development to see what the behavior is and write the logic to support that cmdlet. You could update your code with something like this:

    $isOnLegalHold =get-mailbox $samaccountname  |select litigationholdenabled
    if ($isOnLegalHold) {
        if ($isOnLegalHold -like "*True*")    {
            $isOnLegalHold = $true
            Write-Verbose "$samaccountname set for legalhold"
        }
        else {
            $isOnLegalHold = $false
            Write-Verbose "$samaccountname not set for legalhold"
        } #isOnLegalHold is like true
    }
    else {
        Write-Verbose "No Mailbox found for {0}" -f $samaccountname 
    } #isOnLegalHold is not null
    

You must be logged in to reply to this topic.