[BUG] Remove-PnpUser doesn't throw error when fails
Reporting an Issue or Missing Feature
When the Remove-PnPUser cmdlet fails it prints out the error in red, but doesn't throw any exception that could be catch with try-catch
Expected behavior
When the cmdlet fails, execution should continue on the catch path
Actual behavior
When the cmdlet fails, execution continutes on the catch path
Steps to reproduce behavior
` $sites | % { try { Connect-PnPOnline -Url $_ -ClientSecret $clientsecret -ClientId $clientid -WarningAction Ignore Remove-PnPUser -Identity $usertoremove -Confirm:$false Write-Output "error not caught" } catch { Write-Output "error caught" Write-Output $_ }
} ` for some sites, this script fails (actually, i have no idea why, but that is another question)
Remove-PnPUser : Attempted to perform an unauthorized operation. At line:27 char:1
- Remove-PnPUser -Identity "xxxxx" -Co ...
-
+ CategoryInfo : WriteError: (:) [Remove-PnPUser], ServerUnauthorizedAccessException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Principals.RemoveUser
error not caught
What is the version of the Cmdlet module you are running?
1.11.0
Which operating system/environment are you running PnP PowerShell on?
- [x] Windows
- [ ] Linux
- [ ] MacOS
- [ ] Azure Cloud Shell
- [ ] Azure Functions
- [ ] Other : please specify
You must specify the ErrorAction = Stop if you want the cmdlet the throw an error when an error occurs, if you do not, it is by design that the script continues. Additionally, the ErrorAction = Stop support for PnP.Powershell was just added in the latest prerelease. Try Updating and test again please
Thanks, adding ErrorAction = Stop does the job.