Skip to content

Instantly share code, notes, and snippets.

@etiennetremel
Last active February 28, 2023 12:26
Show Gist options
  • Save etiennetremel/985323209c2f70efbdfd629a230a6438 to your computer and use it in GitHub Desktop.
Save etiennetremel/985323209c2f70efbdfd629a230a6438 to your computer and use it in GitHub Desktop.
Azure AD, get list of active users using az cli Graph REST API
#!/usr/bin/env bash
# This script provide a CSV list of users from an AzureAD directory
# It uses the Graph API and jq
ids=(
abcd123
bcde456
cdef789
)
echo "id,employeeId,displayName,mail,enabled" | tee /tmp/users.csv
for id in ${ids[@]}
do
echo "Processing $id"
user=$(az rest --uri https://graph.microsoft.com/v1.0/users \
--url-parameters \$filter="employeeId eq '"$id"'" \$select=id,displayName,mail,accountEnabled,employeeId)
id=$(echo "$user" | jq -r '.value[0].id')
employeeId=$(echo "$user" | jq -r '.value[0].employeeId')
displayName=$(echo "$user" | jq -r '.value[0].displayName')
mail=$(echo "$user" | jq -r '.value[0].mail')
enabled=$(echo "$user" | jq -r '.value[0].accountEnabled')
if [ "$id" != "null" ]
then
echo "\"$id\",\"$employeeId\",\"$displayName\",\"$mail\",\"$enabled\"" | tee -a /tmp/users.csv
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment