我需要谷歌云的帮助,我正在使用谷歌云做 1 个应用程序。在谷歌云中,我有 1 个 Windows 实例和谷歌云 sdk。我需要一个命令来返回该实例的区域名称。注意 - 我不需要区域列表。我只需要运行我的实例的区域名称。先感谢您。
问问题
6543 次
3 回答
12
tl;博士:
gcloud compute instances list <your instance name> --format 'csv[no-heading](zone)'
. . .
这是在做两件事。这
gcloud compute instances list your-instance-name
部分列出了具有该名称的所有实例,例如
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
your-instance-name europe-west1-c n1-standard-1 1.000.000.001 100.000.000.01 RUNNING
和
--format 'csv[no-heading](zone)'
部分将输出重新格式化为带有标题且仅包含zone
列的表。有关格式化输出的更多信息,请参阅https://cloud.google.com/sdk/gcloud/reference/topic/formats(或gcloud help topic formats
)。
于 2016-02-22T15:12:02.530 回答
2
如果在实例本身上并且您想要获取区域:
gcloud compute instances list --filter="name=('`hostname`')" --format 'csv[no-heading](zone)'
于 2021-01-29T14:53:01.963 回答
1
Jeffrey 的回答是使用 gcloud 命令行工具。如果您更愿意使用 GoogleCloud PowerShell 模块,以下将为您提供区域的 uri:
$zone = (Get-GCEInstance).Where({$_.Name -eq $(hostname)}).Zone
例如,这可能会在 $zone 中填充:
https://www.googleapis.com/compute/v1/projects/my-project-001/zones/us-east1-a
如果您只需要结尾部分(烦人的是,其他 cmdlet 上的参数似乎只需要结尾部分并且 uri 不是有效格式),您可以执行以下操作:
$zone.Substring($zone.LastIndexOf("/")+1)
返回:
us-east1-a
于 2018-06-27T10:45:43.473 回答