Powershell script skipping some users

I have the following script that should run through all identities from Sailpoint IdentityIQ, and remove the membership, but it randomly don’t affect users, we saw in the logs that it process one user correctly and then the next one starts but the script then start with the next user not updating the one before.

Can we add a lock or retry until it’s done?

Here’s the code we already have.

Thank you!