powershell icon indicating copy to clipboard operation
powershell copied to clipboard

[BUG] Remove-PnpUser doesn't throw error when fails

Open vilmarci opened this issue 4 years ago • 2 comments

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

vilmarci avatar Sep 08 '22 09:09 vilmarci

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

Studermarc avatar Sep 08 '22 20:09 Studermarc

Thanks, adding ErrorAction = Stop does the job.

vilmarci avatar Sep 12 '22 17:09 vilmarci