如要查看專案中執行的 VM 摘要,請取得 VM 清單。本文說明如何取得所有區域或特定區域的 VM 清單。
事前準備
-
如果尚未設定驗證,請先完成設定。
驗證可確認您的身分,以便存取 Google Cloud 服務和 API。如要從本機開發環境執行程式碼或範例,可以選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
安裝 Google Cloud CLI。 安裝完成後,執行下列指令初始化 Google Cloud CLI:
gcloud init
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
- Set a default region and zone.
Go
如要在本機開發環境中使用本頁的 Go 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
安裝 Google Cloud CLI。
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Set up authentication for a local development environment。
Java
如要在本機開發環境中使用本頁的 Java 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
安裝 Google Cloud CLI。
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Set up authentication for a local development environment。
Node.js
如要在本機開發環境中使用本頁的 Node.js 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
安裝 Google Cloud CLI。
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Set up authentication for a local development environment。
Python
如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
安裝 Google Cloud CLI。
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Set up authentication for a local development environment。
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
安裝 Google Cloud CLI。
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。
取得 VM 清單
主控台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面:
gcloud
使用
gcloud compute instances list
指令,取得專案中所有可用區所有 VM 的匯總清單:gcloud compute instances list
如要將 VM 清單縮小至特定可用區,請使用先前的指令並加上
--zones
旗標。Go
以下範例會取得專案中所有可用區的執行個體清單:
在試用這個範例之前,請先按照Go這篇 Compute Engine 快速入門指南中的操作說明,使用用戶端程式庫設定環境。詳情請參閱 Compute Engine Go API 參考說明文件。
如要向 Compute Engine 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
您也可以取得特定區域中的執行個體清單:
Java
以下範例會取得專案中所有可用區的執行個體清單:
在試用這個範例之前,請先按照Java這篇 Compute Engine 快速入門指南中的操作說明,使用用戶端程式庫設定環境。詳情請參閱 Compute Engine Java API 參考說明文件。
如要向 Compute Engine 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
您也可以取得特定區域中的執行個體清單:
Node.js
以下範例會取得專案中所有可用區的執行個體清單:
在試用這個範例之前,請先按照Node.js這篇 Compute Engine 快速入門指南中的操作說明,使用用戶端程式庫設定環境。詳情請參閱 Compute Engine Node.js API 參考說明文件。
如要向 Compute Engine 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
您也可以取得特定區域中的執行個體清單:
Python
以下範例會取得專案中所有可用區的執行個體清單:
在試用這個範例之前,請先按照Python這篇 Compute Engine 快速入門指南中的操作說明,使用用戶端程式庫設定環境。詳情請參閱 Compute Engine Python API 參考說明文件。
如要向 Compute Engine 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
您也可以取得特定區域中的執行個體清單:
REST
使用
instances.aggregatedList
方法,取得專案中所有可用區所有 VM 的匯總清單:GET https://compute.googleapis.com/compute/v1/projects/PROJECT/aggregated/instances
將
PROJECT
替換為要從中取得 VM 清單的專案名稱。如要將 VM 清單縮小至特定區域,請使用
instances.list
方法:GET https://compute.googleapis.com/compute/v1/projects/PROJECT/zones/ZONE/instances
更改下列內容:
PROJECT
:要從中取得 VM 清單的專案名稱ZONE
:要從中取得 VM 清單的可用區
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-04 (世界標準時間)。
-