Using the Google Cloud SDK with Powershell
The Google Cloud SDK includes a command line tool called gcloud
that is handy for managing Google Cloud resources. For example, I can use it to observe and modify an App Engine application. Here, I list all the versions of an App Engine application:
Observing the current status of my application is nice, but what if I actually want to do something with this information? The output above looks like a Powershell table, but in fact, it’s just text. How do I parse the output with Powershell?
gcloud
+ ConvertFrom-Json
gcloud
and Powershell’s ConvertFrom-Json
cmdlet were made for each other. gcloud
can format all its output as json, and ConvertFrom-Json
converts json to native Powershell objects.
I put these two features to work for me in the following example.
Deleting Old App Engine Versions
I had an App Engine application with old versions that were no longer running, as indicated by STOPPED
in the SERVING_STATUS
column below. I wanted to delete these old versions.
First, I added the --format json
flag to format gcloud
's output as json. Then, I piped the results to ConvertFrom-Json
to convert the output to Powershell objects:
And then I added a filter to find just the versions that were stopped:
Finally, I called gcloud app versions delete
with all the stopped version ids to delete them:
Conclusion
gcloud
's json output and Powershell’s ConvertFrom-Json
cmdlet make it easy to automate repetitive tasks that manipulate Google Cloud resources. And now that Powershell runs on Linux and Mac, you can use these same techniques on those platforms too!
Here’s a combined statement that does it all in one step: