How to use Google Cloud SDK in Powershell?

About Google Cloud SDK

Google Cloud SDK comprises of a set of utilities that enables DevOps Engineers to perform tasks on the Google Cloud Platform (GCP) like creating Compute Engine, managing applications, etc. This SDK, also known as Software Development Toolkit, has command line tools like cloud, bq and gsutil. To simplify it more, SDKs also include APIs that allow you to communicate with remote services.

How to use Google Cloud SDK

To leverage the functionality of the GCP SDK, you would need to download and install the the SDK package from – https://cloud.google.com/sdk/docs/quickstart

Installing Google Cloud SDK

On finishing the setup, the Cloud SDK will prompt for information related to the GCP account through which the communication will happen securely. Mention your account / email id and then mention the GCP instance that needs to be accessed. This can be updated later as well.

Configuring Google Cloud SDK

Once the configuration is confirmed, you can now access the SDK commands through PowerShell.

How to use Google Cloud SDK in PowerShell

Since we already have authenticated our account, we can now get started by creating PowerShell scripts. Let us assume we need to create a PowerShell script that fetches the current status on Compute Engines with their status (whether they are running or terminated). To create this script, open PowerShell ISE (click here to know more ISE) and write the following code and save the file as “checkVM.ps1“-

$result=gcloud compute instances list –format “[box]”
echo $result

Execute this file by pressing “F5” key or clicking on the Run button on shown below and view the –

PowerShell script to use “gcloud” command for viewing GCP VMs

Understanding the Google Cloud SDK Commands

* Store the “gcloud” command output in a variable, in our example it is “$result

* The “gcloud” command calls the “compute instances” function or to say the utility to return all the VMs / compute engine with their status. The output is formatted in tabular format by using the keyword “–format [box]” and the same is stored in “$result variable in PowerShell.

* The “echo $result” displays the output as shown in the image above.

In case you want to search specific VM details, then the same can be done by adding the keyword “–filter=”app-vm2″“; where “app-vm2” is the name of the VM that needs to be looked at.

$result=gcloud compute instances list –filter=”app-vm2″ –format “[box]”
echo $result

Refer Google SDK documentation for details on these functions and keywords, click here to view..

Hope this blog was useful!! Keep watching this space for some more advanced scripting using PowerShell and Google Cloud SDK.