이 문서에서는 조직 또는 폴더의 각 프로젝트에서 서비스 상태 이벤트 처리를 사용 설정하는 스크립트를 설명합니다. 이 명령어를 사용하면 지정된 Identity and Access Management (IAM) 주 구성원에 서비스 상태 뷰어 역할이 부여되므로 이벤트를 보고 Service Health API를 사용 설정할 수 있습니다.
상위 항목 아래의 프로젝트를 나열할 수 있는 권한:
resourcemanager.projects.list
지정된 IAM 주 구성원에 IAM(서비스 상태 뷰어 역할)을 추가할 권한: resourcemanager.projects.setIamPolicy
Google Cloud 서비스를 사용 설정할 수 있는 권한: serviceusage.services.enable
이러한 권한을 얻는 한 가지 방법은 관리자에게 적절한 역할을 부여해 달라고 요청하는 것입니다. IAM 기본 역할 및 사전 정의된 역할 참조 페이지의 사전 정의된 역할 섹션에서 권한을 검색합니다.
권한이 있는 역할이 표시됩니다.
스크립트 실행
스크립트는 다음 매개변수를 허용합니다.
PARENT_ID: 프로젝트의 상위 ID입니다. ID는 조직 또는 폴더의 ID일 수 있습니다. 상위 항목의 모든 프로젝트에서 Personalized Service Health가 사용 설정됩니다.
(선택사항) IAM_PRINCIPAL: 서비스 상태 뷰어 역할이 부여될 주 구성원 또는 구성원의 식별자입니다. 일반적으로 PRINCIPAL_TYPE:ID 형식을 사용합니다. 예:user:my-user@example.com
지원되는 값의 전체 목록은 프로젝트, 폴더, 조직에 대한 액세스 관리 페이지의 단일 역할 부여 섹션을 참고하세요.
스크립트를 실행하려면 다음 안내를 따르세요.
API VERSION(v1 또는 v1beta)를 결정합니다.
다음 스크립트를 파일에 붙여넣습니다.
#!/bin/bashPARENT_ID="$1"PRINCIPAL="$2"FAILED_PROJECTS=()forprojectin$(gcloudprojectslist--filter="parent.id: ${PARENT_ID}"--format="value(projectId)")doecho"Enabling PSH API for project $project"gcloudservicesenableservicehealth.googleapis.com--project="${project}"echo"Finished enabling PSH API for project $project"if[[-n"$PRINCIPAL"]];thenecho"Adding $PRINCIPAL as service health viewer to project $project"gcloudprojectsadd-iam-policy-binding"${project}"--member"${PRINCIPAL}"--roleroles/servicehealth.viewer
echo"Finished adding $PRINCIPAL as service health viewer to project $project"sleep5elseecho"PRINCIPAL not provided, will not grant service health viewer role. Please provide a PRINCIPAL value in order to view events."fiecho"Attempt to list events from Personalized Service Health for project $project"RESPONSE="$(curl-w"%{http_code}"-H"Authorization: Bearer $(gcloudauthprint-access-token)"-H"Content-Type: application/json"https://servicehealth.googleapis.com/APIVERSION/projects/"${project}"/locations/global/events)"HTTP_CODE=$(tail-n1 <<< "$RESPONSE")if[["$HTTP_CODE"-ne200]];thenecho"Failed to list events for project $project"echo"Response: $RESPONSE"FAILED_PROJECTS+=($project)elseecho"Successfully listed events for project $project"fidoneif[["${#FAILED_PROJECTS[@]}"-ne0]];thenecho"Listing projects that failed to activate"forprojectin"${FAILED_PROJECTS[@]}"doecho"$project"donefi
스크립트를 실행합니다. 다음 예시에서는 스크립트가 activateProjects.sh이라는 파일에 있다고 가정합니다.
조직 ID 345678901의 모든 프로젝트를 활성화하고 사용자 user:test-user@gmail.com에게 roles/servicehealth.viewer 역할을 부여하려면 다음을 실행합니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-09-04(UTC)"],[],[],null,["# Enable Personalized Service Health for all projects in an organization or folder\n\nThis document describes a script that enables each project in an organization\nor folder for service health events processing. It grants the\n[Identity and Access Management (IAM) principal](/iam/docs/overview#how_cloud_iam_works)\nspecified the Service Health Viewer role, which lets you view events and\nenable the Service Health API.\n\nBefore you begin\n----------------\n\n\n[Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\nEnsure that you have the following permissions to run the script:\n\n- Permission to list projects under the parent: `resourcemanager.projects.list`.\n- Permission to add IAM (Service Health Viewer role) for the specified IAM principal: `resourcemanager.projects.setIamPolicy`.\n- Permission to enable Google Cloud services: `serviceusage.services.enable`.\n\nOne way to gain these permissions is to ask an administrator to grant you an\nappropriate role. Search for the permissions in the [Predefined roles](/iam/docs/understanding-roles#predefined)\nsection of the IAM basic and predefined roles reference page.\nThe roles that have the permissions appear.\n\nRun the script\n--------------\n\nThe script accepts the following parameters:\n\n- `PARENT_ID`: ID of the parent to projects. The ID can be for an organization or a folder. All projects under the parent will have Personalized Service Health enabled.\n- (optional) `IAM_PRINCIPAL`: An identifier for the principal, or member, which\n will be granted the Service Health Viewer role. It usually has the following\n form: `PRINCIPAL_TYPE:ID`. Example:`user:my-user@example.com`.\n\n For the full list of supported values, see the\n [Grant a single role](/iam/docs/granting-changing-revoking-access#grant-single-role)\n section of the Manage access to projects, folders, and organizations page.\n\nTo run the script:\n\n1. Decide on the \u003cvar class=\"readonly\" scope=\"API_VERSION\" translate=\"no\"\u003eAPI VERSION\u003c/var\u003e: `v1` or `v1beta`.\n2. Paste the following script to a file:\n\n #!/bin/bash\n\n PARENT_ID=\"$1\" PRINCIPAL=\"$2\"\n\n FAILED_PROJECTS=()\n\n for project in $(gcloud projects list --filter=\"parent.id: ${PARENT_ID}\" --format=\"value(projectId)\")\n do\n echo \"Enabling PSH API for project $project\"\n gcloud services enable servicehealth.googleapis.com --project=\"${project}\"\n echo \"Finished enabling PSH API for project $project\"\n\n if [[ -n \"$PRINCIPAL\" ]]; then\n echo \"Adding $PRINCIPAL as service health viewer to project $project\"\n gcloud projects add-iam-policy-binding \"${project}\" --member \"${PRINCIPAL}\" --role roles/servicehealth.viewer\n echo \"Finished adding $PRINCIPAL as service health viewer to project $project\"\n sleep 5\n else echo \"PRINCIPAL not provided, will not grant service health viewer role. Please provide a PRINCIPAL value in order to view events.\"\n fi\n\n echo \"Attempt to list events from Personalized Service Health for project $project\"\n RESPONSE=\"$(curl -w \"%{http_code}\" -H \"Authorization: Bearer $(gcloud auth print-access-token)\" -H \"Content-Type: application/json\" https://servicehealth.googleapis.com/\u003cvar scope=\"API_VERSION\" translate=\"no\"\u003eAPI\u003cspan class=\"devsite-syntax-w\"\u003e \u003c/span\u003eVERSION\u003c/var\u003e/projects/\"${project}\"/locations/global/events)\" HTTP_CODE=$(tail -n1 \u003c\u003c\u003c \"$RESPONSE\")\n\n if [[ \"$HTTP_CODE\" -ne 200 ]] ; then\n echo \"Failed to list events for project $project\"\n echo \"Response: $RESPONSE\"\n FAILED_PROJECTS+=($project)\n else\n echo \"Successfully listed events for project $project\"\n fi\n done\n\n if [[ \"${#FAILED_PROJECTS[@]}\" -ne 0 ]]; then\n echo \"Listing projects that failed to activate\"\n for project in \"${FAILED_PROJECTS[@]}\"\n do\n echo \"$project\"\n done\n fi\n\n3. Run the script. The following examples assume the script is in a file named\n `activateProjects.sh`:\n\n - To activate all projects in organization ID `345678901` and grant\n user`user:test-user@gmail.com` the role of`roles/servicehealth.viewer`,\n run:\n\n bash activateProjects.sh 345678901 \"user:test-user@gmail.com\"\n\n - To activate all projects in organization ID `345678901` and grant\n service account `serviceAccount:test-proj1@example.domain.com` the role\n of`roles/servicehealth.viewer`, run:\n\n bash activateProjects.sh 345678901 \"serviceAccount:test-proj1@example.domain.com\"\n\nPersonalized Service Health will take up to 24 hours to start processing service health\nevents."]]