Possible faulty Exception name for Remove-MgGroupMemberByRef in both v1.0 and beta
Apologies in advance, still learning Powershell and Graph so I might've missed something obvious.
Tried implementing a try/catch for my script where one of the commands is Remove-MgGroupMemberByRef
When the removed group is not the expected type (Mail-enabled sec or dist list) an error is thrown.

While trying to find the exact ExceptionType i got these results:
$error[0].Exception.GetType().fullname outputs
Microsoft.Graph.PowerShell.Runtime.RestException`1[[Microsoft.Graph.PowerShell.Models.IMicrosoftGraphODataErrorsOdataError, Micr
osoft.Graph.Groups.private, Version=1.10.0.0, Culture=neutral, PublicKeyToken=#16 character code, removed just in case#]]
I tried using both the full output, Microsoft.Graph.PowerShell.Runtime.RestException and Microsoft.Graph.PowerShell.Runtime.RestException`1 in my Catch but with no luck.
Is the exception type that can be used in a catch not supported for this command or am I just approaching this wrongly?
$error[0].Exception.gettype() | select * in case this is helpful. Blocked out any info I'm not sure is sensitive or not.

@MeesVees, you'll need to specify an -ErrorAction Stop on the command you want to catch the exception for as stated at https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-exceptions?view=powershell-7.3#cmdlet--erroraction-stop. All commands in the module throw non-terminating errors by default, customers can use ErrorAction to change this behavior.
You can also catch all System.Exception and handle the returned Microsoft Graph error code via:
try {
Remove-MgGroupMemberByRef -GroupId $GroupId -DirectoryObjectId $MemberId -ErrorAction Stop
} catch [System.Exception] {
if ($PSItem.Exception.Code -eq "EXPECTED_ERROR_CODE_HERE") {
# Handle the error here.
}
}
Thank you for clarifying, Modified your example slightly to be able to handle specific error messages instead of the general one that $psitem.Exception.Code gives (as far as i tested, every command gives Request_BadRequest. Im guessing this is because the commands are just making API calls behind the scenes and this is the status/Error code it receives.
This modified solutions works perfectly for me.
try {remove-mggroupmemberbyref -groupid $groupId -directoryobjectid $MemberId -Erroraction Stop}
catch [System.Exception]
{
if($psitem.exception.message -eq "Cannot Update a mail-enabled security groups and or distribution list.")
{#Do stuff}
}
Just for context why specific errors are needed. I'm working on a offboarding script that wipes the account without actually deleting it and removal of groups is a little tricky with Graph since it only supports the removal of one type of group and for every other type of group we need to use a different command. Final solution will now be a lot clearer and logical since I can handle specific errors instead of throwing everything at the group I'm trying to remove 😄 Thanks again, have a blessed day 🌞