Hi @Anonymous ,
Try below script which first asks for the path to the CSV file containing the Group ID and Service Principal information. It checks if the file exists and then reads the CSV file line by line to populate the groupId_list
and sp_list
arrays. After that, it processes the Service Principals as in the original script, but now the data is read from the CSV file.
#!/bin/bash
# Prompt for the CSV file path
read -p "Enter the path to the CSV file: " csv_file
# Check if the CSV file exists
if [ -f "$csv_file" ]; then
# Read CSV file and populate arrays
while IFS=, read -r GroupID ServicePrincipal; do
groupId_list+=("$GroupID")
sp_list+=("$ServicePrincipal")
done < "$csv_file"
# Process Service Principals
for sp_name in "${sp_list[@]}"; do
spObjId=$(az ad sp list --display-name "$sp_name" --query '[0].id' --output tsv)
if [ -n "$spObjId" ]; then
for groupId in "${groupId_list[@]}"; do
az ad group member add --group "$groupId" --member-id "$spObjId"
if [ $? -eq 0 ]; then
echo "Service Principal with Name: $sp_name (Object ID: $spObjId) added to AAD Group '$groupId' successfully."
else
echo "Failed to add Service Principal with Name: $sp_name (Object ID: $spObjId) to AAD Group '$groupId'."
fi
done
else
echo "Service Principal with Name: $sp_name not found."
fi
done
else
echo "CSV file not found at the specified path."
fi
Note: The above script is generated with the help of an AI service and posted after manually validating the accuracy of the response and making necessary changes as appropriate. However, please test it in your environment and validate if it works as expected or not.