GCP disks.list在PowerShell脚本中执行时偶发报错的解决需求
问题
在本地Windows机器的PowerShell中使用gcloud CLI操作,单独执行以下命令能得到预期输出:
gcloud compute disks list ` --project=prj-d-jenkins-poc ` --filter="-users:*" ` --format="csv(name,sizeGb,zone,status,type,lastAttachTimestamp,lastDetachTimestamp)"
但整合到以下脚本执行时偶发报错:
#Initiated OUTPUT variable as ArrayList to collect results $OUTPUT = New-Object -TypeName "System.Collections.ArrayList" $OUTPUT.Add("name,sizeGb,zone,status,type,lastAttachTimestamp,lastDetachTimestamp") #Get list of accessible projects to PROJECTS $PROJECTS = gcloud projects list --format="value(projectId)" #Iterate through projects list foreach ($i in $PROJECTS) { #Get list of unattached disks $list = gcloud compute disks list ` --project=$i ` --filter="-users:*" ` --format="csv(name,sizeGb,zone,status,type,lastAttachTimestamp,lastDetachTimestamp)" #If list is not empty, add it to OUTPUT if ($list.count -gt 0){ $list = [System.Collections.ArrayList]$list #Convert a to ArrayList $list.RemoveAt($list.0) #Remove first entry for a $OUTPUT += $list #append a to OUTPUT Write-Host "Unattached disks in $i wrote to OUTPUT variable." } else{ #If list is empty, skip Write-Host "No unattached disks found in $i." } } $OUTPUT
报错信息如下:
python.exe : WARNING: The following filter keys were not present in any resource : users At C:\Users\minh.tran\AppData\Local\Google\Cloud SDK\google-cloud-sdk\bin\gcloud.ps1:117 char:3 + & "$cloudsdk_python" $run_args_array + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (WARNING: The fo...esource : users:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError
推测原因是遍历到无任何磁盘(包括已挂载和未挂载)的项目时,users字段不存在,触发该警告,需要修改代码忽略这类项目。
解决方法
1. 提前检查项目是否存在磁盘资源
在执行带过滤条件的disks.list命令前,先快速判断当前项目是否有磁盘,避免在无磁盘项目上执行含users字段的过滤命令:
$hasDisks = gcloud compute disks list --project=$i --format="value(name)" --limit=1 if (-not $hasDisks) { Write-Host "No disks found in project $i, skipping." continue }
2. 直接抑制警告输出
如果不想提前检查,可将gcloud命令的警告/错误流重定向到空,避免控制台报错:
$list = gcloud compute disks list ` --project=$i ` --filter="-users:*" ` --format="csv(name,sizeGb,zone,status,type,lastAttachTimestamp,lastDetachTimestamp)" ` 2>$null
2>$null会将PowerShell中的标准错误流(包含gcloud的警告信息)重定向,不在控制台显示。
3. 优化后的完整脚本
结合以上两种方式,优化脚本逻辑并提升效率:
# Initialize OUTPUT variable as ArrayList to collect results $OUTPUT = New-Object -TypeName "System.Collections.ArrayList" $null = $OUTPUT.Add("name,sizeGb,zone,status,type,lastAttachTimestamp,lastDetachTimestamp") # Get list of accessible projects $PROJECTS = gcloud projects list --format="value(projectId)" # Iterate through projects list foreach ($i in $PROJECTS) { # Check if project has any disks first $hasDisks = gcloud compute disks list --project=$i --format="value(name)" --limit=1 if (-not $hasDisks) { Write-Host "No disks found in project $i, skipping." continue } # Get list of unattached disks, suppress warnings $list = gcloud compute disks list ` --project=$i ` --filter="-users:*" ` --format="csv(name,sizeGb,zone,status,type,lastAttachTimestamp,lastDetachTimestamp)" ` 2>$null # If list is not empty, add it to OUTPUT if ($list.Count -gt 0) { $list = [System.Collections.ArrayList]$list $list.RemoveAt(0) # Remove header row $OUTPUT.AddRange($list) # Use AddRange for more efficient appending Write-Host "Unattached disks in $i added to OUTPUT variable." } else { Write-Host "No unattached disks found in $i." } } $OUTPUT
注:
- 使用
$null = $OUTPUT.Add(...)避免输出Add方法的返回值 - 用
AddRange替代+=提升ArrayList的追加效率 - 提前检查磁盘存在性,减少不必要的过滤命令执行
内容的提问来源于stack exchange,提问作者Minh Tran
相关产品推荐
相关产品推荐

