0

I've installed the gcloud CLI according to https://cloud.google.com/sdk/docs/install.

When using cloud shell on browser, I could simply paste a script and it would work. But it won't do the same when using cloud CLI on Powershell.

Script:

# 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

Result on browser cloud shell: successfully iterated through projects and listed disks in that project.

Result on 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> 
Minh Tran
  • 89
  • 1
  • 7
  • Your question is too broad and asks for recommendations. Instead, present a problem to solve that can be answered with facts and/or citations. There are many ways to interact with Google Cloud. That included the browser console GUI, Cloud Shell, SDKs, third-party tools, etc. In your case install the CLI on your desktop and run the commands locally. You can save the output to files and use editors and other tools to parse and process the output. https://stackoverflow.com/help/on-topic – John Hanley Sep 22 '22 at 05:58
  • Thanks for replying John! I actually installed the CLI and tried it not so long ago based on a blog post on your website. For some reasons, the scripts I used on console cloud shell no longer works on the CLI. :( Apologize if the question seems elementary. I'm not a developer and just started dabbling into this recently. – Minh Tran Sep 22 '22 at 06:11
  • My suggestion is to edit your question into a specific problem that you have using the CLI. That way you are clearly defining a problem that can be answered. I will be happy to try and help you. – John Hanley Sep 22 '22 at 06:56
  • As John proposed, I think in your case the most simpler way is to install `gcloud` sdk on your machine. Normally your `shell` scripts should work as in `Cloud shell`. – Mazlum Tosun Sep 22 '22 at 08:38
  • Thanks for the input. I've edited my question to reframe the problem to gcloud CLI. :) – Minh Tran Sep 23 '22 at 03:27
  • Please use text and not images. The first screenshot is just black. The second has red text. I cannot read (see) text in the color red. https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question/285557#285557 – John Hanley Sep 23 '22 at 04:29
  • I've editted the question again for better clarity. :) – Minh Tran Sep 26 '22 at 14:36
  • 1
    You are trying to run a Linux **shell** script as PowerShell on Windows. That will **not** work. There are command shells for Windows, but you should run this script from a Linux machine. – John Hanley Sep 26 '22 at 14:47

1 Answers1

1

The comment from @John Hanley is the correct answer. I tried to use a Linux shell script as a PowerShell script on a Windows machine.

The simplest solution for my case is to convert the shell script to a PowerShell script and run the converted script from PowerShell.

Converted script can be found here: GCP | disks.list method returning error when ran as part of a script . Despite working, it is still throwing some errors.

Minh Tran
  • 89
  • 1
  • 7