I know how to create users via command line:
azure-cli
az login
(which is interactive)az ad user create --display-name <NAME> --password <PASS> --user-principal-name <PRINCIPAL>
Now I want to make it non-interactive.
I've found that if you want non-interactive login you can create a Service Principal and log in to it as explained here. However as I understand Service Principals have different sets of permissions than users, which are assigned by roles (AFAIK built-in are listed here). I've tried to create Service Principal with the highest (Owner) permissions, but it still cannot create users.
In AWS to solve analogous problem (log in non-interactively to AWS and create users in AWS IAM) you just log in non-interactively as a user:
aws-cli
aws iam create-user --user-name <NAME>
and pass Access key in AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
variables.However I cannot find a way to non-interactively login to Azure as a user. I've found only how to log in non-interactively as a Service principal and I don't know how to give it permission to add Microsoft Entra ID users.
I have two questions:
Note that, Azure RBAC roles are different from Microsoft Entra roles. To create users, you need to either assign directory roles or Microsoft Graph permissions.
For non-interactive login to Azure CLI, you can make use of service principal authentication.
Initially, create one app registration and add User.ReadWrite.All
permission of Application type by granting admin consent to it as below:
Now, create one client secret in above app registration and note it's value:
You can get App ID and Tenant ID values of application's Overview page:
To login to Azure CLI as service principal, make use of below command:
az login --service-principal -u <app-id> -p <client-secret> --tenant <tenantId>
Response:
As you already granted User.ReadWrite.All
Application permission with admin consent to service principal, you can directly run below command to create user:
az ad user create --display-name <NAME> --password <PASS> --user-principal-name <PRINCIPAL>
Response:
Alternatively, you can also assign "Directory Writer" or "User Administrator" Microsoft Entra roles to service principal like this:
Go to Azure Portal -> Microsoft Entra ID -> Roles and administrators -> User administrator -> Add assignment -> Select service principal