Set up a Service principle to a AAD group by Bash Scripting

Emran Hossain 195 Reputation points
2023-10-10T08:19:09.5+00:00

I need a Help to solve it by bash Scripting . Have a look a condition for Script :

I have a bash Script where i put the group id & list of Service Principle to that group & it works fine & ok but i want to modify that Script to like below:

Script want to ask me what group you to insert here > " Put in the Group Id or Name"
Script want to ask me what Service Principle you to insert to that group > "Put the Service Principle name or ID"
Paste my Whole below :

groupId="ff9cf2bd-38d5-49ab-af7a-3ac4f7fba81a" sp_list=("emran-test-demo-03" "emran-test-demo-04" "TurbonomicSP")

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
    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 successfully."
    else
        echo "Failed to add Service Principal with Name: $sp_name (Object ID: $spObjId) to AAD Group."
    fi
else
    echo "Service Principal with Name: $sp_name not found."
fi
done

Please someone come to help me .

Emmi
Azure DevTest Labs
Azure DevTest Labs
An Azure service that is used for provisioning development and test environments.
258 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,140 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,791 questions
0 comments No comments
{count} votes

Accepted answer
  1. 2023-10-12T03:27:48.8533333+00:00

    Hello @Emran Hossain , to read bash values into variables you have to use the read command like this:

    printf "Put in the Group Id or Name\n"
    read groupIdOrName
    printf $groupIdOrName
    
    printf "Put the Service Principle name(s) or ID(s)\n"
    read -a sp_list
    for sp in ${sp_list[@]}
    do
        echo $sp
    done
    

    Let us know if you need additional assistance. If the answer was helpful, please accept it and rate it so that others facing a similar issue can easily find a solution.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful