Created
February 8, 2024 17:21
-
-
Save clemlesne/83f721935cd779575ff0784369261f3e to your computer and use it in GitHub Desktop.
Add users to Azure Entra ID and add them to a group, in a batch.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
CSV_FILE="users.csv" | |
# Skip the header line | |
tail -n +2 "$CSV_FILE" | while IFS=',' read -r first_name last_name hackaton_group_number first_password | |
do | |
# Create the user in Azure AD, make sure the user principal name is in lowercase | |
display_name="$first_name $last_name" | |
user_principal_name="$(echo $first_name.$last_name | tr '[:upper:]' '[:lower:]')@XPBDF.onmicrosoft.com" | |
group_name="hackathon-group-$hackaton_group_number" | |
# Check if the user already exists, or create a new one | |
user_id=$(az ad user show --id "$user_principal_name" --query "id" -o tsv 2>/dev/null) | |
if [ -n "$user_id" ]; then | |
echo "User '$display_name' already exists with ID '$user_id'" | |
else | |
user_id=$(az ad user create \ | |
--display-name "$display_name" \ | |
--force-change-password-next-sign-in \ | |
--password "$first_password" \ | |
--query "id" \ | |
--user-principal-name "$user_principal_name" \ | |
-o tsv) | |
echo "Created user '$display_name' with ID '$user_id'" | |
fi | |
# Test user creation | |
if [ -z "$user_id" ]; then | |
echo "Failed to create user '$display_name', skipping group assignment" | |
continue | |
fi | |
# Find the group's object ID based on the group name | |
group_id=$(az ad group show \ | |
--group "$group_name" \ | |
--query "id" \ | |
-o tsv) | |
# Add user to the group | |
az ad group member add \ | |
--group "$group_id" \ | |
--member-id "$user_id" | |
echo "Added '$display_name' to group '$group_name'" | |
done | |
echo "All users processed" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
first_name | last_name | hackaton_group_number | first_password | |
---|---|---|---|---|
Clemence | Lesne | 1 | Password123 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment