Search code examples
pythonazureazure-active-directoryazure-automationazure-sdk-python

How to get list of users who are having owner access for a azure subscription using python


I am trying to get the list of users who are having owner access for a subscription.

I tried checking for python azure sdk. But am not getting any api which does this functionality.

Subscription list api is available but it is not providing details of users who are having access to the particular subscription.

I tried the below code

subscriptionClient = SubscriptionClient(credentials)
for subscription in subscriptionClient.subscriptions.list():
    print (subscription)

Any help would be appreciated


Solution

  • this PowerShell command :

    (Get-AzureRmRoleAssignment -RoleDefinitionId "8e3af657-a8ff-443c-a75c-2fe8c4bcb635" -Scope "/subscriptions/<your azure sub ID>" | where {($_.ObjectType -EQ "user") -and ($_.Scope -EQ "/subscriptions/<your azure sub ID>") }  ) | select DisplayName,SignInName
    

    will return all Azure AD users with subscription owner role.

    I have tried to captured data packages about this ps command, and it called multiple rest APIs to finish this process. You can host this command on Azure App service webjobs, Azure function or Azure automation and explore a webhook to get the user list when you need it. Hope it helps.