1

I have a scenerio where a User is on Hold and he doesn't have OneDrive but only have Mailbox. If that's the case then I'm trying to update only for the Mailbox but some reason I'm getting an error something like " That person doesn't have OneDrive" and it's stop executing/updating.

I thought my try catch block already handle it to excute the script even if a user only have Mailbox but I'm not sure why I'm getting that error and not excuting.

How can I skip that error and only update user Mailbox anyway?. I'm not sure where I'm doing it wrong in my try catch block

try {
    #Update Hold
    $holdPolicy = Get-CaseHoldPolicy -identity $usr.GK -ErrorAction Stop
    Write-Host "Hold Existed for $usr.GK"
    if ($holdPolicy.isValid) {
        #if the hold exist, compare their SMTP address to exchange Location
        if ($holdPolicy.ExchangeLocation -ne $usr.PrimarySmtpAddress) {

            #if the hold exist, Add or Update the hold.
            $status = Get-CaseHoldPolicy -Identity $usr.GK -DistributionDetail
            if ($status.DistributionStatus -ne "Pending") {
                # policy no longer pending

                $usr.GK + "HOLD Existed, Update Hold"
                try {
                    # Try to Update both Mailbox and OneDrive
                    Write-Host ""
                    Set-CaseHoldPolicy -Identity $usr.GK  -AddSharePointLocation $domain -AddExchangeLocation $usr.PrimarySmtpAddress -Comment $newComm

                }
                catch {
                    try {
                        #IT's not picking this for some reason
                        #If OneDrive not existed then UPDATE Mailbox only
                        Set-CaseHoldPolicy -Identity $usr.GK -AddExchangeLocation $usr.PrimarySmtpAddress -Comment $newComm 
                        Write-Host "Updated Mailbox only"
                    }
                    catch {

                        try {
                            #If Mailbox not existed then UPDATE OneDrive only
                            Set-CaseHoldPolicy -Identity $usr.GK -AddSharePointLocation $domain -Comment $newComm 

                        }
                        catch {
                            #If Both doesn't existed then put the name
                            Set-CaseHoldPolicy -Identity $usr.GK -Comment $newComm 

                        }

                    }

                }
            }
            else {
                # policy still pending
                Write-Host "Hold policy still pending."
            }

    
        } 

    }
}Catch{
    #Add New HOLD


    # SOME CODE


}
ziico
  • 449
  • 8
  • 23
  • 3
    I'm haven't used that module yet, but you should add the parameter `-ErrorAction Stop` to all cmdlets you use (like you did for the first cmdlet call in line 3). Otherwise it will not trigger the catch block. – swbbl Feb 01 '22 at 04:47
  • @swbbl ohhh man.. you are right. it work. stupid me for forgetting that. lol Thanks really appreciate it – ziico Feb 01 '22 at 04:49
  • 2
    If you want to `Stop` on any cmdlet, you could look in setting `$ErrorActionPreferance = 'Stop'`. Have a look on [preference](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.2) variables. – Vivere Feb 01 '22 at 08:56
  • In short: `try` / `catch` only acts on _terminating_ errors, whereas it is far more typical for cmdlets to report _non-terminating_ errors. For the `catch` block to get invoked, non-terminating errors must be promoted to terminating ones by passing `-ErrorAction Stop` or by setting `$ErrorActionPreference = 'Stop'` beforehand. See the [linked answer](https://stackoverflow.com/a/59641995/45375) for more information. – mklement0 Feb 01 '22 at 14:06

0 Answers0