PowerShell with GCP — Google Cloud Platform

Google Cloud SDK provides libraries and tools for interacting with Google Cloud products and services. It includes Google Cloud Command Line Interface (gcloud CLI) for various platforms like Linux, Windows, MacOS.

On Windows, you can even use PowerShell to manage GCP because the SDK has dozens of cmdlets for this purpose.

To install the SDK on Windows, you can

  1. Either download the Google Cloud CLI installer in exe format and run it
  2. Or using PowerShell
(New-Object Net.WebClient).DownloadFile("https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe", "$env:Temp\GoogleCloudSDKInstaller.exe")

& $env:Temp\GoogleCloudSDKInstaller.exe

3. Or using the PowerShellGet module to install from PowerShell Gallery

PS D: ~Temp> install-module GoogleCloud

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
 InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install
the modules from 'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y


PS D: ~Temp> Get-Command |Where-Object {$_.Source -eq 'GoogleCloud'}

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        gs:                                                1.0.1.10   GoogleCloud
Cmdlet          Add-GcdChange                                      1.0.1.10   GoogleCloud
Cmdlet          Add-GcdManagedZone                                 1.0.1.10   GoogleCloud
Cmdlet          Add-GceAddress                                     1.0.1.10   GoogleCloud
Cmdlet          Add-GceFirewall                                    1.0.1.10   GoogleCloud
Cmdlet          Add-GceHealthCheck                                 1.0.1.10   GoogleCloud

With this way, the SDK is still needed and when you try to invoke any GoogleCloud cmdlets, it will prompt to install the SDK. Once you answer “Y” to confirm the installation, the interactive installer (a GUI interface) is launched. It also installs two things in addition to Google Cloud CLI Core Libraries and Tools.

  1. Bundled Python (both version 2 & 3)
  2. Cloud Tools for PowerShell

Cloud Tools for PowerShell is a collection of cmdlets for accessing and manipulating Google Cloud resources such as Google Compute Engine, Google Cloud Storage, Google Cloud SQL and Google Cloud DNS etc.

PS D: ~Temp> Get-Command Resize-GceDisk
Google Cloud SDK is not found in PATH. The SDK is required to run the module.

Installing Google Cloud SDK
Do you want to install Google Cloud SDK? If you want to force the installation without prompt, set
$env:GCLOUD_SDK_INSTALLATION_NO_PROMPT to true or add '-quiet' to Import-Module -ArgumentList.
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):

Installing Google Cloud SDK interactively
Do you want to use the interactive installer? Select no to install silently on the command line.
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y

WARNING: You will have to restart the shell and/or run 'gcloud init' (if you haven't run it after installing the SDK) before the module can be used.

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Resize-GceDisk                                     1.0.1.10   GoogleCloud

After the installation, “gcloud init” is automatically run to initiate authentication and authorization for tools.

Welcome to the Google Cloud CLI! Run "gcloud -h" to get the list of available commands.
---
Welcome! This command will take you through the configuration of gcloud.

Your current configuration has been set to: [default]

You can skip diagnostics next time by using the following flag:
  gcloud init --skip-diagnostics

Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.
Reachability Check passed.
Network diagnostic passed (1/1 checks passed).

You must log in to continue. Would you like to log in (Y/n)?  Y

Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=32555940559.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8085%2F&scope=openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fsqlservice.login+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth&state=rqltDhtaHUGuIRSCAJMrAqSnlsEE1n&access_type=offline&code_challenge=QdvZmnw11sA0V6pJ7kszxP0ICIab4iN_VDWREprz9Lk&code_challenge_method=S256

You are logged in as: [xxxxx@gmail.com].

Pick cloud project to use:
 [1] alert-nimbus-348800
 [2] Enter a project ID
 [3] Create a new project
Please enter numeric choice or text value (must exactly match list item):  1

Your current project has been set to: [alert-nimbus-348800].

Do you want to configure a default Compute Region and Zone? (Y/n)?  Y

Which Google Compute Engine zone would you like to use as project default?
If you do not specify a zone via a command line flag while working with Compute Engine resources, the default is
assumed.
 [1] us-east1-b
 [2] us-east1-c
 [3] us-east1-d
.
.
.
 [49] asia-northeast2-c
 [50] asia-northeast3-a
Did not print [54] options.
Too many options [104]. Enter "list" at prompt to print choices fully.
Please enter numeric choice or text value (must exactly match list item):  1

Your project default Compute Engine zone has been set to [us-east1-b].
You can change it by running [gcloud config set compute/zone NAME].

Your project default Compute Engine region has been set to [us-east1].
You can change it by running [gcloud config set compute/region NAME].

Created a default .boto configuration file at [D:\Users\awnmc\.boto]. See this file and
[https://cloud.google.com/storage/docs/gsutil/commands/config] for more
information about configuring Google Cloud Storage.
Your Google Cloud SDK is configured and ready to use!

* Commands that require authentication will use xxxxx@gmail.com by default
* Commands will reference project `alert-nimbus-348800` by default
* Compute Engine commands will use region `us-east1` by default
* Compute Engine commands will use zone `us-east1-b` by default

Run `gcloud help config` to learn how to change individual settings

This gcloud configuration is called [default]. You can create additional configurations if you work with multiple accounts and/or projects.
Run `gcloud topic configurations` to learn more.

Some things to try next:

* Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command.
* Run `gcloud topic --help` to learn about advanced features of the SDK like arg files and output formatting
* Run `gcloud cheat-sheet` to see a roster of go-to `gcloud` commands.

Once the initialization is done, restart your CMD window or PowerShell or Windows Terminal so that environment variables PATH takes effect.

The Google Cloud SDK also has to two shortcuts for CMD (Google Cloud SDK Shell) and PowerShell (Cloud Tools for PowerShell) created which have enviroment variables loaded first when launched from there. But what I’ve found is that it is not necessary to use them, normal Windows Terminal (PowerShell or CMD) will work as well.

Examples to start/stop a compute instance in Google Cloud:

PS D: ~Temp> Get-GceInstance

Name       CpuPlatform          MachineType Zone          TimeCreated
----       -----------          ----------- ----          -----------
instance-1 Unknown CPU Platform e2-micro    us-central1-a 2022-04-29T18:27:56.672-07:00

PS D: ~Temp> Get-GceInstance|Select-Object name,status

Name       Status
----       ------
instance-1 TERMINATED

PS D: ~Temp> Start-GceInstance -name instance-1 -Zone us-central1-a

Name       CpuPlatform MachineType Zone          TimeCreated
----       ----------- ----------- ----          -----------
instance-1 AMD Rome    e2-micro    us-central1-a 2022-04-29T18:27:56.672-07:00


PS D: ~Temp> Get-GceInstance|Select-Object name,status

Name       Status
----       ------
instance-1 RUNNING

PS D: ~Temp> Stop-GceInstance -name instance-1 -Zone us-central1-a

Name       CpuPlatform          MachineType Zone          TimeCreated
----       -----------          ----------- ----          -----------
instance-1 Unknown CPU Platform e2-micro    us-central1-a 2022-04-29T18:27:56.672-07:00

PS D: ~Temp> Get-GceInstance|Select-Object name,status

Name       Status
----       ------
instance-1 TERMINATED

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s