1. Overview
PowerShell is a command-line shell and associated scripting language built on the .NET Framework. It's the default task automation and configuration management tool used in the Windows world. A PowerShell cmdlet is a lightweight command invoked within 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 —with more to come!
Follow along with this lab to learn how to interact with Google Cloud resources from PowerShell.
What you'll learn
- How to install Cloud Tools for PowerShell.
- How to authenticate with the Google Cloud SDK.
- How to create and manage Google Compute Engine from PowerShell.
- How to backup data to Google Cloud Storage from PowerShell.
What you'll need
How will you use this tutorial?
How would rate your experience with Google Cloud Platform?
2. Setup and Requirements
For this codelab, you need a Google Cloud project to interact with PowerShell. If you have an existing project, you can either use that or you can create a new project using the following steps.
Self-paced environment setup
If you don't already have a Google Account (Gmail or GSuite), you must create one. Sign-in to Google Cloud console ( console.cloud.google.com) and create a new project:
Remember the project ID, a unique name across all Google Cloud Platform projects. It will be referred to later in this codelab as PROJECT_ID
.
Next, you'll need to enable billing in Google Cloud Console in order to use Google Cloud Platform resources like Google Cloud Datastore and Cloud Storage.
New users of Google Cloud Platform are eligible for a $300 free trial. Running through this codelab shouldn't cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running (see the "cleanup" section at the end of this document).
3. Installation
Self-paced environment installation
PowerShell cmdlets come as part of the Cloud SDK for Windows. If you haven't already done so, download and install Google Cloud SDK for Windows, as described in the Quickstart for Windows guide. Make sure that you've checked the PowerShell option when installing Cloud SDK.
4. Authentication
Cloud Tools for PowerShell uses the credentials and settings stored in the Cloud SDK. So in order to start using the cmdlets, you first need to login using the Cloud SDK.
Self-paced environment authentication
Start Google Cloud SDK Shell and run the following command.
gcloud init
Accept the option to log in using your Google user account.
To continue, you must log in. Would you like to log in (Y/n)? Y
In your browser, log in to your Google user account when prompted and click Allow to grant permission to access Google Cloud Platform resources.
At the command prompt, select a Cloud Platform project from the list of those where you have Owner, Editor or Viewer permissions.
Pick a cloud project to use:
[1] [my-project-1]
[2] [my-project-2]
...
Please enter your numeric choice:
5. Run Google Cloud cmdlets within PowerShell
Once authenticated, you're ready to use Google Cloud cmdlets within PowerShell. Find and open PowerShell command-line shell.
To make sure Google Cloud cmdlets are installed correctly, run Get-GceInstance
cmdlet. This should list the Compute Engine instances you have in your project.
6. Create and Manage Google Compute Engine instances
In this section, you create and manage Google Compute Engine from within Google Cloud Tools for PowerShell.
Create an instance configuration
Before creating an instance, you must first create an instance configuration. At minimum this requires a name, a machine type, and a boot disk image or pre-existing boot disk. Use Get-GceImage
to create a disk image and New-GceInstanceConfig
to create a configuration.
$disk = Get-GceImage "windows-cloud" -Family "windows-2012-r2"
$config = New-GceInstanceConfig "my-vm-1" `
-MachineType "n1-standard-4" `
-DiskImage $disk
At this point, you have a configuration that you can use to create an instance.
Create an instance
Use the Add-GceInstance
cmdlet to create a new Compute Engine instance. You can specify a project and zone, but if omitted, the parameter values default to those in your current active Cloud SDK configuration. If you specify project, make sure to replace PROJECT_ID
with your own project id.
$config | Add-GceInstance -Project PROJECT_ID -Zone europe-west1-b
Manage an instance
You can use the Get-GceInstance
cmdlet to retrieve a project's virtual machine instances. Since the instance name may not be unique across projects or zones, you can specify a Project or Zone parameter to narrow the search. By default the cmdlet uses whatever values are in the active Cloud SDK configuration.
$instance = Get-GceInstance "my-vm-1"
You can set instance tags, disks, access configs, and other metadata after creating your instance with the Set-GceInstance
cmdlet. Add some metadata and then remove as follows.
Set-GceInstance $instance -AddMetadata @{"newKey" = "newValue"}
Set-GceInstance $instance -RemoveMetadata "newKey"
Set-GceInstance $instance -RemoveTag "beta" -AddTag "alpha"
Now, add a tag that you will use later to remove the instance.
Set-GceInstance $instance -AddTag "to-be-removed"
You can start, stop, or restart an instance using various cmdlets. You can refer to an instance by using the name or the strongly-typed object returned from the Get-GceInstance
cmdlet. Play with some of these commands.
Stop-GceInstance $instance
Start-GceInstance $instance
Restart-GceInstance "my-vm-1"
Finally, when you are finished with the instance, remove it from Compute Engine by using the Remove-GceInstance
cmdlet and supplying the tag you specified earlier.
Get-GceInstance -Project $project |
Where { $_.Tags.Items -contains "to-be-removed" } |
Remove-GceInstance -WhatIf
7. Backup data to Google Cloud Storage
In this part of the codelab, you back up data from your local machine to Google Cloud Storage using Cloud Tools for PowerShell.
Create a bucket
Before uploading files to Cloud Storage, you need to create a bucket. Use the New-GcsBucket
cmdlet to create a new bucket.
$bucket = "my-gcs-bucket"
New-GcsBucket $bucket
Upload files to the bucket
You can use New-GcsObject
to upload file or entire folders to the bucket.
You can upload the contents of a local file to Cloud Storage by using the -File
parameter and specifying a file path. Alternatively, you can pass the object's contents as a string via the PowerShell pipeline, or you can use the -Value
parameter.
Pick a local file on your machine and upload to your bucket as follows.
New-GcsObject -Bucket "my-gcs-bucket" -File "C:\path\to\some\file\hello.txt"
You can upload an entire directory from the local disk to Cloud Storage by using the -UploadFolder
parameter and specifying the folder path. If you do not want the folder to be uploaded directly to the root of the Cloud Storage bucket, use -ObjectNamePrefix
to specify a prefix that will be applied to every object uploaded.
Pick a local folder on your machine and upload to your bucket as follows.
New-GcsObject -Bucket "my-gcs-bucket" -Folder "C:\path\to\some\folder"
Search data
You can search data with cmdlets, or with the provider through the common file search cmdlets. Try the following command with your bucket.
Get-GcsObject $bucket | Select Name, Size | Out-GridView
You should see a grid pop-up with name and size.
Read data
You can use the Read-GcsObject
cmdlet to read data. As an example, you can use the following command to read a file named hello.txt
to your desktop.
Read-GcsObject $bucket "hello.txt" `
-OutFile "$Env:UserProfile\Desktop\hello.txt"
Delete data
Last but not least, you can delete data using the Remove-GcsObject
cmdlet. Use the following command to delete all contents of the bucket.
Get-GcsObject $bucket | Remove-GcsObject
8. Congratulations!
In this codelab, you learned how to manage Compute Engine instances and Cloud Storage buckets from PowerShell but there's more! You can also manage Cloud SQL and Cloud CDN resources using PowerShell. Check out the list of next steps below to learn more.
What we've covered
- How to install Cloud Tools for PowerShell.
- How to authenticate with the Google Cloud SDK.
- How to create and manage Google Compute Engine from PowerShell.
- How to backup data to Google Cloud Storage from PowerShell.
Next Steps
- Learn more about Cloud Tools for PowerShell.
- Learn more about Windows on Google Cloud Platform.
- Learn more about .NET on Google Cloud Platform.
- Learn more about SQL Server on Google Cloud Platform.
- Learn more about Cloud Tools for Visual Studio.