Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Cloud Tool for PowerShell
2.1.
Authenticate with gcloud CLI
2.2.
Run PowerShell cmdlets
2.3.
Navigate Cloud Storage with PowerShell provider
3.
Backing up data to Cloud Storage
3.1.
Uploading Data
3.2.
Upload files to a bucket
3.3.
Searching data
3.4.
Reading data
3.5.
Deleting data
4.
Creating and managing Google Kubernetes Engine clusters
4.1.
Creating and updating GKE Cluster
4.2.
Creating and Maintaining Node Pools
5.
Working with Cloud DNS
5.1.
Creating a managed zone for your domain
5.2.
Adding and Removing resource record sets
5.3.
Creating resource record sets
5.4.
Retrieving resource record sets
5.5.
Applying changes to a managed zone
5.6.
Deleting managed zones
6.
Setting Up Cloud SQL Instances
6.1.
Creating a new instance
6.2.
Creating a read replica instance
6.3.
Creating a failover replica instance
7.
Managing Instance Replications
7.1.
Starting and stopping replication
7.2.
Promoting a read replica instance
7.3.
Activating a failover
8.
Managing Instance Backups
8.1.
Reverting to a Backup
8.2.
Removing Backup Runs
9.
Importing and Exporting Data
9.1.
Importing SQL and CSV files
9.2.
Exporting SQL and CSV files
10.
Frequently Asked Questions
10.1.
What is the purpose of PowerShell?
10.2.
Is PowerShell coding or scripting?
10.3.
What is GCP Tool?
11.
Conclusion
Last Updated: Mar 27, 2024

Overview of Tools for PowerShell

Author Sagar Mishra
1 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

PowerShell is a cross-platform task automation key made up of a scripting language, a Command line shell, and a configuration management framework. PowerShell runs on Windows, macOS, and Linux.

This blog will discuss the topic Overview of Tools for PowerShell in detail. Let's dive without wasting your time.

Cloud Tool for PowerShell

Our first topic in the "Overview of Tools for PowerShell" series is Cloud Tool for PowerShell. Here, we will discuss Authentication, Run PowerShell cmdlets, and Navigation using Cloud Storage.

Follow the below steps before you start.

  1. Create an account in case you are new to Google Cloud.
     
  2. Select or create a Google Cloud project on the project selector page in the Google Cloud console.
     
  3. Ensure that billing is enabled for your Cloud project.
     
  4. Install the gcloud using the below command.
Install-Module GoogleCloud

Authenticate with gcloud CLI

The cmdlets are made available by Cloud Tools for PowerShell using your login details and settings from the gcloud CLI. Use the gcloud CLI to log in before you can use the cmdlets:

Step 1: Run the given command in the command prompt.

gcloud init

 

Step 2: Accept the given option to log in using your Google gcloud user account:

 

Step 3: Click Allow to grant permission after logging in to your Google user account when prompted In your browser.

 

Step 4: Select a Google Cloud project in which you have Owner, Editor, or Viewer permissions at the command prompt.

Run PowerShell cmdlets

You can start using the Cloud Tools for PowerShell after authenticating. Run the following commands in the PowerShell window.

Get all buckets for the active project, a specific project, or a specified bucket:

$currentProjBuckets = Get-GcsBucket
$specificProjBuckets = Get-GcsBucket -Project my-project-1
$bucket = Get-GcsBucket -Name my-bucket-name

 

List all Compute Engine instances linked with a project:

Run PowerShell cmdlets

Navigate Cloud Storage with PowerShell provider

You can start Cloud Tools for PowerShell using the shortcut from the start menu to directly use the provider.

# Navigate to Cloud Storage
cd gs:\
# Show the available buckets
ls
# Create a new bucket
mkdir my-new-bucket

 

By importing Cloud Tools for the PowerShell module using "Import-Module GoogleCloud," you can also make the Cloud Storage provider accessible in any PowerShell session.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

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"

Searching data

You can search data with cmdlets through the common file search cmdlets.

Get-GcsObject $bucket | Select Name, Size | Out-GridView

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"

Deleting data

Use the standard Remove-Item cmdlet to delete data through the provider.

Get-GcsObject $bucket | Remove-GcsObject

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 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 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 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"

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 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 RegistryOverview of Azure Sphere ApplicationsOverview 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 problemsinterview experiences, and interview bundle for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

thankyou

Live masterclass