Issue with try/catch and nested if/else

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

$LegalHoldStatus =get-mailbox $samaccountname  |select litigationholdenabled
if ($LegalHoldStatus -like "*True*")
Write-Host "$samaccountname set for legalhold"
$LegalHoldStatus = " "
Write-Host "$samaccountname not set for legalhold"
$LegalHoldStatus = " "
Write-Host "error finding mailbox for litigationhold for $samaccountname ignore if this is a secondary account" -foregroundcolor "Red"

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?

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?

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.

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…

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

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

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