Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Script to create AWS Identity Center users from a file and add them to the "students" group
- # Usage: ./create_users.sh <emails_file>
- set -e
- # Check if filename is provided
- if [ $# -eq 0 ]; then
- echo "Usage: $0 <emails_file>"
- echo "Example: $0 emails.txt"
- exit 1
- fi
- EMAILS_FILE="$1"
- # Check if file exists
- if [ ! -f "$EMAILS_FILE" ]; then
- echo "Error: File '$EMAILS_FILE' not found!"
- exit 1
- fi
- # Get the Identity Store ID
- echo "Fetching Identity Store ID..."
- IDENTITY_STORE_ID=$(aws sso-admin list-instances --query 'Instances[0].IdentityStoreId' --output text)
- if [ -z "$IDENTITY_STORE_ID" ]; then
- echo "Error: Could not retrieve Identity Store ID"
- exit 1
- fi
- echo "Identity Store ID: $IDENTITY_STORE_ID"
- # Find the "students" group ID
- echo "Finding 'students' group..."
- GROUP_ID=$(aws identitystore list-groups \
- --identity-store-id "$IDENTITY_STORE_ID" \
- --filters AttributePath=DisplayName,AttributeValue=students \
- --query 'Groups[0].GroupId' \
- --output text)
- if [ -z "$GROUP_ID" ] || [ "$GROUP_ID" == "None" ]; then
- echo "Error: 'students' group not found!"
- exit 1
- fi
- echo "Students group ID: $GROUP_ID"
- echo ""
- echo "Starting user creation..."
- echo "=========================="
- # Read emails from file and create users
- while IFS= read -r email || [ -n "$email" ]; do
- # Skip empty lines
- [ -z "$email" ] && continue
- # Trim whitespace
- email=$(echo "$email" | xargs)
- echo "Processing: $email"
- # Extract username from email (part before @)
- username=$(echo "$email" | cut -d'@' -f1)
- # Split username into first and last name
- # If username contains a dot, split by dot (e.g., javi.moreno)
- if [[ "$username" == *.* ]]; then
- FIRST_NAME=$(echo "$username" | cut -d'.' -f1 | sed 's/\b\(.\)/\u\1/g')
- LAST_NAME=$(echo "$username" | cut -d'.' -f2- | sed 's/\b\(.\)/\u\1/g')
- else
- # If no dot, use the whole username as first name and "User" as last name
- FIRST_NAME=$(echo "$username" | sed 's/\b\(.\)/\u\1/g')
- LAST_NAME="User"
- fi
- # Create user
- echo " Creating user: $FIRST_NAME $LAST_NAME ($email)"
- USER_ID=$(aws identitystore create-user \
- --identity-store-id "$IDENTITY_STORE_ID" \
- --user-name "$username" \
- --display-name "$FIRST_NAME $LAST_NAME" \
- --name Formatted="$FIRST_NAME $LAST_NAME",FamilyName="$LAST_NAME",GivenName="$FIRST_NAME" \
- --emails Value="$email",Type=work,Primary=true \
- --query 'UserId' \
- --output text 2>&1)
- if [ $? -eq 0 ] && [ -n "$USER_ID" ] && [ "$USER_ID" != "None" ]; then
- echo " ✓ User created with ID: $USER_ID"
- # Add user to students group
- echo " Adding user to 'students' group..."
- aws identitystore create-group-membership \
- --identity-store-id "$IDENTITY_STORE_ID" \
- --group-id "$GROUP_ID" \
- --member-id UserId="$USER_ID" \
- --output text > /dev/null 2>&1
- if [ $? -eq 0 ]; then
- echo " ✓ User added to 'students' group"
- else
- echo " ✗ Failed to add user to group"
- fi
- else
- echo " ✗ Failed to create user: $USER_ID"
- fi
- echo ""
- done < "$EMAILS_FILE"
- echo "=========================="
- echo "User creation completed!"
Advertisement
Add Comment
Please, Sign In to add comment