Import Resources from Excel sheet/CSV file to Azure portal by Azure cli/Bash Script

Emran Hossain 195 Reputation points
2023-10-20T13:19:28.0333333+00:00

Hope everyone are doing great . I am trying to do a Bash Script from import & Export CSV /excel file for multiple group to group member as a Service Principle or Managed Identity . But i do not know how to import & Export file by Azure Cli/bash . Someone look & help me to fix it up . I am insert my Bash Script & CSV below here :

CSV file like this :

User's image

Bash Code :


Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,366 questions
Windows for business Windows Server User experience PowerShell
Microsoft Security Microsoft Entra Microsoft Entra ID
{count} votes

1 answer

Sort by: Most helpful
  1. tbgangav-MSFT 10,426 Reputation points Moderator
    2023-10-20T14:17:02.5133333+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.