簡體   English   中英

如何讓 shell 腳本在 PowerShell 中的 gcloud CLI 上工作?

[英]How to get shell script to work on gcloud CLI in PowerShell?

我已經根據https://cloud.google.com/sdk/docs/install安裝了 gcloud CLI。

在瀏覽器上使用雲 shell 時,我只需粘貼一個腳本即可。 但在 Powershell 上使用雲 CLI 時,它不會做同樣的事情。

腳本:

# List Projects accessible to these credentials
PROJECTS=$( `
  gcloud projects list `
  --format="value(projectId)")

# Iterate over each Project
for PROJECT in ${PROJECTS} 
do
  echo "Project: ${PROJECT}"
  # Check Compute Engine service
  ENABLED="$( `
    gcloud services list `
    --project=${PROJECT} `
    --filter=config.name=compute.googleapis.com `
    --format='value(state)')"
  # Is it enabled?
  if [ "${ENABLED}" = "ENABLED" ]
  then
    # Enumerate Disks that have `users` and output `name`
    gcloud compute disks list `
    --project=${PROJECT} `
    --filter="-users:*" `
    --format="csv(name,sizeGb,zone,status,type,lastAttachTimestamp,lastDetachTimestamp)"
  fi
done

瀏覽器雲shell上的結果:成功迭代項目並列出該項目中的磁盤。

Powershell 的結果:

PS C:\WINDOWS\System32> C:\Users\minh.tran\Documents\Get Disk.ps1
At C:\Users\minh.tran\Documents\Get Disk.ps1:7 char:4
+ for PROJECT in ${PROJECTS}
+    ~
Missing opening '(' after keyword 'for'.
At C:\Users\minh.tran\Documents\Get Disk.ps1:8 char:3
+ do
+   ~
Missing statement body in do loop.
At C:\Users\minh.tran\Documents\Get Disk.ps1:17 char:5
+   if [ "${ENABLED}" = "ENABLED" ]
+     ~
Missing '(' after 'if' in if statement.
At C:\Users\minh.tran\Documents\Get Disk.ps1:17 char:7
+   if [ "${ENABLED}" = "ENABLED" ]
+       ~
Missing type name after '['.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword
 

PS C:\WINDOWS\System32> 

@John Hanley 的評論是正確答案。 我嘗試在 ZAEA23489CE3AA9B6406EBB28E 機器上使用 Linux shell 腳本作為 PowerShell 腳本。

對於我的情況,最簡單的解決方案是將 shell 腳本轉換為 PowerShell 腳本,然后從 PowerShell 運行轉換后的腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM