I am trying to get a list of all GCP projects on the domain and the project owners and export it to a CSV so I can throw it into a google sheet. Getting a list is simple enough, but I can't find a way to get the owners for each project.
ROLE="roles/owner"
for PROJECT in $(\
gcloud projects list \
--format="value(projectId)" \
--filter="projectId ~ something")
do
printf "%s:\n" ${PROJECT}
gcloud projects get-iam-policy ${PROJECT} \
--flatten="bindings[].members[]" \
--filter="bindings.role=${ROLE}" \
--format="value(bindings.members)"
printf "\n"
done
For completeness, using the excellent jq which is both more general-purpose and -- I think -- easier to use:
for PROJECT in $(\
gcloud projects list \
--format="value(projectId)" \
--filter="projectId ~ something")
do
printf "%s:\n" ${PROJECT}
gcloud projects get-iam-policy ${PROJECT} --format="json" \
| jq -r '.bindings[] | select(.role=="roles/owner") | .members[]'
printf "\n"
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With