AN INTRODUCTION TO POWERSHELL (AUTOMATION & DATABASE)

image.png

What Is PowerShell:

In a simple term, PowerShell basically is an interactive command line environment built on .NET Framework which automate tasks that are supposed to be repetitive, long and most probably annoying. You can execute programs known as "SCRIPT" which contains various cmdlets (classes, methods, objects, types, etc.)

NOTE: before we proceed, you should know that PowerShell is not case sensitive so you can either type in caps or small letters.

Cmdlet In Details:

A cmdlet is a command through which we carry out an action in the PowerShell environment. PowerShell follows a verb-noun naming system, i.e. A command must have a verb(what it dose) and name (what it's called), some instances are: Get-Help, Get-Process, Get-Member.

Theses are some of the commands available in PowerShell:

image.png

Classes:
A class is a parameter for an object. Classes describes how an object should look like and the things it can actually do & how to create one.

Below is an illustration of how to create a class in PowerShell:

image.png

Objects:
Objects are basically the cmdlet representation of actions we want to automate in PowerShell; objects are usually classified into two: properties (which explains the attributes of what the object is representing) then once again, we basically methods (these are basically blocks of code that carry out specific actions as we discussed above ). In order to create an object, we could use the type accelerator [ ] which represents the class, then followed by a default function that comes with every class called new().

Types:

basically types are part of the description of an object. This basically means, that a type is a description of what properties or members an object must have. For instance, if we run a Get-Process, we get a bunch of objects, and these objects are "processinfo type of objects". Meaning the type of process that the computer is currently running & they all have the same properties. Here's a break down of powershell itself: Core Language Runtime (CLR) -- .Net -- Libraries -- Powershell -- Powershell Modules.

In summary: types are the definition of all members in an object.

A type contains objects, the object contains properties.

To represent a type in PowerShell, follow this syntax: [include your type in here]

For instance, to reference a sting type we would do it this way: [string]. We could still store values in this way:

$a(you could use any thing in place of a to store value. In this case I chose a) = 'joe, ball, game'

[string]$a ($ takes the data's stored in a and then converts it to a string, this is called casting)

Typing these on a single line we have: $a = 'joe, ball, game '
[string]$a then press the enter key.

If you have a type you ain't familiar with, you can run: $a. GetType()

Note Properties:
Note properties are created by PowerShell, they do not have any specific type and can fit in anywhere you put them

Properties:
These properties have a specific type and name, they have restrictions that allows only the appropriate types in.

METHODS A.K.A FUNCTIONS:
Are simply a block of code that does a specific action. Methods allows us to perform some action on a certain object, just like properties methods are shared across types, i.e every process has the same process methods, every file object has the same methods etc.

Just like commands, methods can have parameters on them.

The parameters of a method only take in specific types.

image.png

Now Let's Discuss The PowerShell Environment (PowerShell lSE vs PowerShell )

PowerShell ISE is basically a graphical user interface (GUI) that enables us to execute, create and modify commands & scripts. PowerShell. Meanwhile The PowerShell is an environment or command line interface (CLI), where we can write and execute our code.

Here is what a PowerShell ISE environment looks like:

image.png

Here's what a PowerShell environment looks like:

image.png

Variable:
In a general term, a variable is a unit of memory in which data are stored. In PowerShell, we can represent a variable by writing it out this way: $a = 1, $4 = $4.00 "john". What this means here is that any character that has "$" before it, becomes a variable

Data Types Available In PowerShell:

They include: integers, strings, floating point value or decimals, Booleans and datetime values. The GetType method returns the current data type of the given variable.

Integers:

They're whole numbers, any available decimal gets rounded off.

Strings:

They're a set of characters or alphabetical letters.

Floating Point Value Or Decimals:

They're decimal values.

Booleans:

They're values that can either be true or false.

Datetime Values:

They're values that typically contains time.

Note: variables may as well be converted from one type to the other by "explicit conversion".

Arrays & Hashtable:
Arrays are used to store a list of data or values, it could be of any data type and they're mostly similar. Hashtable on the other hand, a more advanced form of array that is used in storing data that is related to each other and mostly compliment each other.

Here's an array declaration:

image.png

Here's a hashtable declaration:

image.png

NOTE: There are lots of things that I know I didn't cover in this series, however; I would do my best to put out another breakdown series where I talk about so many terms in the simplest form possible but until then please refer to get-help cmdlet, try to read it through it's not really that long:

image.png

image.png

To access the web browser, type in: get-help get-help -online in your PowerShell environment and hit enter or simply go to your browser and copy and paste this link :
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/get-help?view=powershell-5.1&WT.mc_id=ps-gethelp

image.png

Sort:  

Your content has been voted as a part of Encouragement program. Keep up the good work!

Use Ecency daily to boost your growth on platform!

Support Ecency
Vote for new Proposal
Delegate HP and earn more

Congratulations @easternface! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You received more than 300 upvotes.
Your next target is to reach 400 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Christmas Challenge - Offer a gift to to your friends
Support the HiveBuzz project. Vote for our proposal!