Search code examples
c#azureazure-cli

Exception types from az cli commands


I got a C# code which invokes an az cli command : "az ad group member add ..."

How can I know which types of exceptions can be thrown from that command? Specifically, I need the type of the exception when trying to add a user which is already in the group, but I would like to know in general where can I find documentation of possible exceptions from az commands.

This is the documentation of the command which doesn't specify possible exceptions:

https://learn.microsoft.com/en-us/cli/azure/ad/group/member?view=azure-cli-latest#az-ad-group-member-add


Solution

  • In general, Azure CLI commands can throw exceptions related to authentication, authorization, network connectivity, and other issues like invalid parameters depending on that specific context/scenario.

    Currently, there is no specific documentation of possible exceptions from az commands. You need to explicitly check the error message

    In my case, I tried to execute the command with a user who is already in the group to find what type of exception is thrown.

    I have one Azure AD group named SriGroup with below group members:

    enter image description here

    When I tried to add user who is already in group using Azure CLI command, I got exception as One or more added object references already exist for the following modified properties: 'members'

    az ad group member add --group "SriGroup" --member-id "8710bd50-78b7-45b0-9d43-xxxxxx"
    

    Response:

    enter image description here

    When I ran below Microsoft Graph query that is equivalent to above command, I got same error:

    POST https://graph.microsoft.com/v1.0/groups/groupId/members/$ref/
    {
        "@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/userobjId"
    }
    

    Response:

    enter image description here

    You can also run the command by adding --verbose at the end to know what API it is calling in backend:

    az ad group member add --group "SriGroup" --member-id "8710bd50-78b7-45b0-9d43-xxxxxx" --verbose
    

    Reference: Microsoft Graph error responses and resource types