Home > Software engineering >  Error when trying to get-iam-policy gcloud
Error when trying to get-iam-policy gcloud

Time:02-21

I'm trying to get an IAM policy from some specific list of projects in CSV file using this bash script:

#! /bin/bash
echo "Getting IAM list from accounts:"
sleep 4
while read -r projectId || [ -n $projectId ] 
do
gcloud projects get-iam-policy ${projectId}
echo $projectId
done < NonBillingAccountGCP.csv

But I'm getting this error:

ERROR: (gcloud.projects.get-iam-policy) INVALID_ARGUMENT: Request contains an invalid argument.
<project-ID-from-csv>

If I'm running this script using the project-id it does work and print all IAM policies.

Any idea?

Thanks!

CodePudding user response:

I suspect the error results from the first line heading (PROJECT_ID or similar) in your CSV.

You can use awk to drop the first line and for a slightly cleaner variant:

FILE="NonBillingAccountGCP.cs"
PROJECTS=$(awk "NR>1" ${FILE})
for PROJECT in ${PROJECTS}
do
  echo ${PROJECT}
  gcloud projects get-iam-policy ${PROJECT}
done

This format also allows you to compose gcloud projects list:

PROJECTS=$(gcloud projects list \
--filter=... \
--format="value(projectId)")

CodePudding user response:

The issue was with the CSV format (DOS instead of Unix).

  • Related