Backing up data to Cloud Storage
Our next topic in the "Overview of Tools for PowerShell" series is Backing Up data to Cloud Storage. Here, we will learn how to backup data from a local machine to Cloud Storage using Cloud Tools for PowerShell.
Uploading Data
Cloud Storage Data is organized into buckets. We can create a new bucket using the below command.
$bucket = "my-gcs-bucket"
New-GcsBucket $bucket

You can also try this code with Online Python Compiler
Run Code
Upload files to a bucket
Use the New-GcsObject command to upload files to a bucket.
# Upload the folder LogFiles and its content to the root of the widget bucket.
New-GcsObject -Bucket "widget" -Folder "C:\inetpub\logs\LogFiles"
# Upload the folder LogFiles and its content to the directory Test in the widget bucket.
New-GcsObject -Bucket "widget" -Folder "C:\inetpub\logs\LogFiles" -ObjectNamePrefix "Test"

You can also try this code with Online Python Compiler
Run Code
Searching data
You can search data with cmdlets through the common file search cmdlets.
Get-GcsObject $bucket | Select Name, Size | Out-GridView

You can also try this code with Online Python Compiler
Run Code
Reading data
Use the standard Get-Content cmdlet To read data through the provider.
Read-GcsObject $bucket "timestamp.txt" | Write-Host
Read-GcsObject $bucket "logo.png" `
-OutFile "$Env:UserProfile\pictures\logo.png"

You can also try this code with Online Python Compiler
Run Code
Deleting data
Use the standard Remove-Item cmdlet to delete data through the provider.
Get-GcsObject $bucket | Remove-GcsObject

You can also try this code with Online Python Compiler
Run Code
Creating and managing Google Kubernetes Engine clusters
In the "Overview of Tools for PowerShell" series, our next topic is creating and managing google Kubernetes engine clusters.
Google Kubernetes Engine is a strong cluster manager and orchestration system for running your Docker containers. The GKE tool schedules your containers into the cluster and manages them automatically based on the requirements.
Creating and updating GKE Cluster
Follow the below commands to create and update your GKE cluster:
# Creates a GKE Node Config with image type CONTAINER_VM
# and 20 GB disk size for each node.
$nodeConfig = New-GkeNodeConfig -DiskSizeGb 20 `
-ImageType CONTAINER_VM
# Creates a cluster named "my-cluster" in the default zone of the
# default project using config $nodeConfig and network "my-network".
Add-GkeCluster -NodeConfig $nodeConfig `
-ClusterName "my-cluster" `
-Network "my-network"

You can also try this code with Online Python Compiler
Run Code
You can create a cluster using the Add-GkeCluster cmdlet's options when handing it the NodeConfig object.
# Creates a cluster named "my-cluster" with the description "my new cluster."
# in the default zone of default project using machine type
# "n1-standard-4" for each Compute Engine in the cluster.
# The cluster will use the subnetwork "my-subnetwork."
# The cluster's nodes will have autoupgrade enabled.
# The cluster will also autoscale its node pool to a maximum of 2 nodes.
Add-GkeCluster -MachineType "n1-standard-4" `
-ClusterName "my-cluster" `
-Description "My new cluster" `
-Subnetwork "my-subnetwork" `
-EnableAutoUpgrade `
-MaximumNodesToScaleTo 2

You can also try this code with Online Python Compiler
Run Code
You can also update a cluster with the Set-GkeCluster cmdlet. Only one property of the clusters can be updated at a time.
# Setting additional zones of cluster "my-cluster" in zone "asia-east1-a"
# to the zones "asia-east1-b" and "asia-east1-c". This means the clusters will
# have nodes created in these zones. The primary zone
# ("asia-east1-a") will be added to the
# AdditionalZone array by the cmdlet.
Set-GkeCluster -ClusterName "my-cluster" `
-Zone "asia-east1-a" `
-AdditionalZone "asia-east1-b", "asia-east1-c"

You can also try this code with Online Python Compiler
Run Code
You can also list available clusters with the Get-GkeCluster cmdlet.
# Lists all container clusters in the default project.
Get-GkeCluster
# List all container clusters in zone "us-central1-a"
# of the default project.
Get-GkeCluster -Zone "us-central1-a"

You can also try this code with Online Python Compiler
Run Code
Creating and Maintaining Node Pools
Follow to below commands to create and maintain node pools:
# Creates a node pool named "my-nodepool" with image type
# CONTAINER_VM for each node.
$nodePool = New-GkeNodePool -NodePoolName "my-nodepool" `
-ImageType CONTAINER_VM
# Adds the pool to cluster "my-cluster."
Add-GkeNodePool -NodePool $nodePool -Cluster "my-cluster"

You can also try this code with Online Python Compiler
Run Code
You can also list all the node pools in a cluster with the Get-GkeNodePool cmdlet.
# Lists all node pools in cluster "my-cluster" in the default project.
Get-GkeNodePool -ClusterName "my-cluster"
You can remove a node pool from a cluster with the Remove-GkeCluster cmdlet.
# Removes the node pool "my-nodepool" in cluster "my-cluster"
# in the zone "us-west1-b" of the default project.
Remove-GkeCluster -ClusterName "my-cluster" `
-Zone "us-west1-b" `
-NodePoolName "my-nodepool"
Working with Cloud DNS
In the "Overview of Tools for PowerShell" series, we will now learn to work on Cloud DNS. Here, we will discuss configuring a domain's DNS settings using the Cloud DNS and Tools for PowerShell.
Creating a managed zone for your domain
Follow the below commands to set up a managed zone to organize the DNS records that you will create for the domain name.
Add-GcdManagedZone `
-Name "my-new-zone" `
-DnsName "example.com." `
-Description "This is my first zone."
This will create a new zone with the specified details and adds it to the current project of the active gcloud CLI configuration.
Adding and Removing resource record sets
DNS resource records inform the DNS server on how to respond to requests for a domain. DNS records can be used, among other things, to tell a server of the IP address that a domain resolves to and the mail exchange servers that a domain is capable of using.
There are two ways to add or remove immutable resource record sets. Instead, you use the New-GcdResourceRecordSet and Get-GcdResourceRecordSet cmdlets to create independent resource records or obtain already-existing ones.
Creating resource record sets
To create a resource record set, you can use the helper cmdlet New-GcdResourceRecordSet that you can put within a change and then add to or remove from a managed zone.
$ARecord = New-GcdResourceRecordSet `
-Name "example.com." -Rrdata "107.1.23.134" -Type "A"
Similarly,
$CNAMERecord = New-GcdResourceRecordSet `
-Name "www.example.com." -Rrdata "example.com." -Type "CNAME"
Retrieving resource record sets
You can use the given Get-GcdResourceRecordSet cmdlet to return all records in a zone and index the results:
$allRecords = Get-GcdResourceRecordSet -Zone "my-new-zone"
$record0 = $allRecords[0]
You can filter the results accordingly if you only want records of a specific type:
$ARecord = Get-GcdResourceRecordSet -Zone "my-new-zone" -Filter "A"
Applying changes to a managed zone
You can use the Add-GcdChange cmdlet to add records ]or remove them from anything:
Add-GcdChange `
-Zone "my-new-zone" -Add $record1,$record2 -Remove $record0
Deleting managed zones
You can remove a managed zone from your project by using the Remove-GcdManagedZone cmdlet:
Remove-GcdManagedZone -Zone "user1-zone"
The command will return nothing if successful.
Setting Up Cloud SQL Instances
In the "Overview of Tools for PowerShell" series, we will now learn how to set up cloud SQL instances.
A MySQL database running in the cloud is called a Cloud SQL instance. Your MySQL databases can be replicated, stored, and protected using Cloud SQL instances. The instance's behavior can be configured, including when and where data will be replicated and when database maintenance is permitted.
Creating a new instance
You can create a new instance by using the Add-GcSqlInstance cmdlet:
Add-GcSqlInstance $instance
Creating a read replica instance
Read replica instances offer replication functionality for the data in a master instance. They can be turned into a failover or their standalone instance after being created. This configures settings clear to read replica instances, like how fast they replicate data or if they replicate from an external instance:
$setting = New-GcSqlSettingConfig "db-n1-standard-1"
$replicaConfig = New-GcSqlInstanceReplicaConfig
$instance = New-GcSqlInstanceConfig "mynewreplica" `
-SettingConfig $setting `
-ReplicaConfig $replicaConfig `
-MasterInstanceName "gootoso"
Add-GcSqlInstance $instance
Creating a failover replica instance
Cloud SQL uses failover replicas to provide high availability configuration for instances. For the master instance gootoso, the following code snippet generates a new failover replica called myfailover. The -FailoverTarget flag has been added, as well:
$setting = New-GcSqlSettingConfig "db-n1-standard-1"
$replicaConfig = New-GcSqlInstanceReplicaConfig -FailoverTarget
$instance = New-GcSqlInstanceConfig "myfailover" `
-SettingConfig $setting `
-ReplicaConfig $replicaConfig `
-MasterInstanceName "gootoso"
Add-GcSqlInstance $instance
Managing Instance Replications
In the "Overview of Tools for PowerShell" series, we will now learn how to manage instance replications:
Starting and stopping replication
In an instance's read replica instance(s), you can start or stop data replication. The given code snippet starts replication for the read replica instance myreplica:
Start-GcSqlReplica "myreplica"
You can similarly stop this by using the below command:
Stop-GcSqlReplica "myreplica"
Promoting a read replica instance
Use the cmdlet ConvertTo-GcSqlInstance to promote a read replica instance.
ConvertTo-GcSqlInstance "myreplica"
Activating a failover
The below-given code snippet activates the failover replica myfailover:
$instance = Get-GcSqlOperation -Instance "myfailover"
$settingVersion = $instance.Settings.SettingsVersion
Invoke-GcSqlInstanceFailover "myfailover" $settingVersion
The settingVersion contains a version number in the above code snippet.
Managing Instance Backups
In the "Overview of Tools for PowerShell" series, we will now learn how to manage instance backups:
Reverting to a Backup
An instance backup can be restored to return to a prior set of data. This is helpful if something goes wrong while submitting the data. Any replica instances will automatically receive the restoration.
You can restore the most recent backup run, for instance, gootoso:
# Restore to the last backup run
$backup = Get-GcSqlInstanceBackup "gootoso" | Select-Object -first 1
Restore-GcSqlInstanceBackup $backup.Id "gootoso"
Removing Backup Runs
For each instance, Cloud SQL keeps up to seven backup runs. Second Generation instance backups use the same amount of storage as First generation instance backups but are charged at a lower price. It may be a good plan to remove a specific backup run if the instance no longer requires it.
Follow the code to remove the backup run for ID 203949:
Remove-GcSqlBackupRun "gootoso" 203949
Importing and Exporting Data
In the "Overview of Tools for PowerShell" series, we will now learn how to import and export databases and tables to and from Cloud SQL instances:
Make sure the relevant permissions are set up for both importing and exporting. As an Owner, you must add the Service account email address of the instance.
$instance = Get-GcSqlOperation -Instance "mynewinstance"
$serviceEmail = $instance.ServiceAccountEmailAddress
Importing SQL and CSV files
The below snippet imports the data in a local CSV file into the table destinationTable inside of the database destinationDatabase in the Cloud SQL instance gootoso:
Import-GcSqlInstance "gootoso" "C:\Users\User\file.csv" `
"destinationDatabase" "destinationTable"
Exporting SQL and CSV files
You can also export existing databases in a Cloud SQL instance into an existing Cloud Storage bucket for further analysis, importing into other instances, and so on.
Follow the below code for exporting SQL and CSV files:
Export-GcSqlInstance "gootoso" "gs://bucket/file.gz" `
-Databases "guestbook","Purchases"
Frequently Asked Questions
What is the purpose of PowerShell?
PowerShell is a powerful tool. It enables users to search, filter, and export data about the devices on a network as well as automate a variety of boring or time-consuming administrative chores. By combining commands, referred to as "cmdlets," and writing scripts, this is accomplished.
Is PowerShell coding or scripting?
The PowerShell scripting language is an Object Oriented Programming language (OOP) associated with the PowerShell command-line shell. It means that it uses objects to transfer data. These objects have attributes and define custom types using familiar Object Oriented Programming semantics, such as classes, properties, methods, inheritance, etc.
What is GCP Tool?
The Google Cloud Platform (GCP) is a collection of cloud computing services that Google offers. It uses the same internal infrastructure as Google for its consumer products like Google Search, Gmail, Drive, and YouTube.
Conclusion
We have discussed the topic of Overview of Tools for PowerShell. We have seen how to work on Overview of Tools for PowerShell. We discussed how to backup data, create and manage GKE, Work on Cloud DNS, and many more.
We hope this blog has helped you enhance your knowledge of "Overview of Tools for PowerShell." If you want to learn more, check out our articles Overview of Artifact Registry, Overview of Azure Sphere Applications, Overview of Cloud Code, and many more on our platform Coding Ninjas Studio.
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundle for placement preparations.
However, you may consider our paid courses to give your career an edge over others!
Happy Learning!
