AN INTRODUCTION TO POWERSHELL PART 2 (AUTOMATION, DATABASE AND SCRIPITING).

in Programming & Dev3 years ago (edited)

image.png

For this article, I would be constantly interchanging PS and PowerShell as the case may be. Just bear in mind that they are both the same thing. Moving on, in the previous article I made about PowerShell, it was impossible for me to include everything info I wanted to pass across just in one episode so I promised us I was gonna make another episode where I do my best to break down complex terms into simple forms. Let's get right into it:

I believe you saw me use characters like "@, *, $, ( ), |" etc. In the previous episode. However, I never explained what they really were nor what they do. So in this episode, we are going to take a close look at what these characters are, how they function, how PowerShell functions, continue exploring from where we stopped last time and so much more.

Operators: "@( ), *, {} , (), + , -" these special characters are called operators. In PowerShell, operators are characters that can be used in commands or expressions. They tell the compiler to perform a specific task and produce the final result for us.

PowerShell supports the following operators:

ARITHMETIC OPERATORS:
Add-Member lets us add note properties. "+, -, *, /, % '' are used to calculate values of data types in a given command. You can add, subtract, multiply, divide, and return the remainder of a data type.

ASSIGNMENT OPERATORS:
"=, +=, -=, *=, /=, %=" these operators are used to assign and change values to a variable.

ARRAY SUBEXPRESSION OPERATOR @( ) :
This operator returns the result of one or more commands as an array.

COMPARISON OPERATORS:
"-ne, -eq, -gt, -lt, -le, -ge '' these operators are used to compare data types and test conditions to know whether they are true or not, equal or not, etc.

LOGICAL OPERATORS:
"-and, -or, -xor, -not, !" these operators are used to connect conditional statements into a singular complex condition.

SUBEXPRESSION OPERATOR $( ) :
This operator does a special job of returning one or more statements for a single command result, it can as well return results for multiple commands.

PIPELINE OPERATOR | :
This operator passes the result of a command to another command that follows it for further results.

BACKGROUND OPERATOR & :
This operator runs the pipeline before in the background. It returns an object as an output. It can as well be used as a command terminator.

CAST OPERATOR [ ]:
This operator converts objects to a particular data type.

NOTE: refer to: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.2 for further details

image.png

Now that we have cleared that up, lets take a close and critical look at how PowerShell actually functions in all of its layers:

The CLR (CORE LANGUAGE RUNTIME) is situated at the very core part of the PowerShell system, this is where all PowerShell objects comes from and we are able to use these objects to run commands because the CLR provides an entity called the "JIT COMPILER". The JIT Compiler collects Garbage, i.e. Clears unused memory. Moving on, right on top of the CLR is the .NET Library.
.NET provides us with thousands of objects and codes for various things like ways to access and work with files, ways to work with file encoding, hundreds of library functions etc. Under the .NET is the "LIBRARY", a library is an entity that adds its own objects and code to the available objects in .NET. For instance DLL files in windows are all libraries. Now PowerShell is directly housed on top of the .NET itself. This is where it was built and its core in other words, it is what makes PowerShell be what it is. Finally right on top of all these entities is the "PowerShell Modules" they're just like libraries, however the difference is that they're made to extend
PowerShell i.e. They are their own PowerShell commands and they make use of PowerShell's objects.

image.png

Moving forward, let us dive right into some other important terms.

PRIMITIVES: can be said to be a special type of object that has no properties that can be easily broken down. Primitive objects are made inside the CLR (core language runtime), bear in mind that the CLR is the core of .NET . The list of primitives hosted on the CLR are: numbers, Booleans, strings, arrays.

HERE IS A REAL LIFE INSTANCE OF A PRMITIVE: int32. This is a full break down of how it is stored in the computer memory : it takes 32bits & 4bytes of space in memory. That is to say, that every whole number is a primitive and they are collectively called integers or int32.

SCRIPT: the word script in PS, basically means a file containing PowerShell code in it. With that being explained, let's talk about scripts in details:

To create a PS script, this time directly from pc without any external text editor:
Open notepad, go to file.

Select the location where you would like to save it.

Select save-as give it whatever name you like and after the name add a ".ps1" this extension tells the computer that you are creating a PS script.

When you want to run your script, right click on the script and select run with PowerShell or you could directly run it from the PowerShell environment itself by doing the following:

cd PSDemo (cd means you are referring to the PowerShell directory generally known as PSDemo). Next is to type out the script name:

./script.ps1

NOTE: by default, PS doesn't allow us to run any kind of script we want, for security concerns so in order to bypass this restriction, type in the following command in PowerShell: set-Execution Policy Unrestricted hit enter, close and restart PS.

STATIC MEMBERS:
Unlike non-static members, these members are shared across all objects of the same type. The unique thing about them is that they're aren't found on individual objects of the same type; rather, they're on the actual type and they're custom made per type. Explaining further in detail, static members are like functions; they don't belong to any particular group of objects. They most usually are called on the go, they return data or create data etc. and that's the end. Let's take a look at "static DateTime new() property" this property here, accepts one command then either sets or gets data in return. To access a static property or the static properties available in a type , we specify they type followed by two colons and then the name of the thing; just like this:

[type] : : thing .

Here's a practical example:

Using DateTime as an example, let us get its static members first, to get that we have to write the command this way: [DateTime ] | Get-Member -static.

Note: we can get the static member of any type using the same method above.

image.png

Now, let us access a static property in the Int type:he

For this example, we chose the static int maxvalue property (I highlighted it so we can easily spot it). Observe the pattern we followed which is Type first, followed by two colons :: and finally the thing we wanna access.

image.png

Furthermore, there are some types that are only static in nature. This means that you can't make an object out of them; one of these types is the type "Environment". It only contains a bunch of static types telling us about environment

COLLECTIONS:
Collections are things made up of several smaller objects. Some types of collections are:

An array (I covered this in the last episode).

Another type of collection is a HashTable. Although I introduced a HashTable in the previous episode, this time let us take some time to understand it better. Unlike an array, where we access the data in it using this "@()", to access the data in a HashTable table we use this "@{}". Furthermore, in order to access specific data in a HashTable we use a pattern called accessing by KEY and VALUE. Let's take a look at an instance for better understanding:

Assuming we have a HashTable containing a list of human names and age, in order to get the age of a specific name in the table, we would need to use a name from the table to access the age. That means that each name is a key pointing to an age which is a value.

Now in order to find out the age of a certain person in the table, we would have to write out the command this way "$age['John']" in order to create a HashTable we would have to write the command this way "@{John = 28; Bob =35}"

image.png

image.png

That would be the end for now, In the next coming episode I would give you a couple more definitions, and then we would start making scripts using all we have covered so far, and after we would build a full app with a graphical user interface using PowerShell, I am excited already, I hope you are too!! Until next time, if you have any questions feel free to ask and I will do my best to respond on time.

Sort:  

This really nice. I recommend, you also put in some free learning links to add further value, to the topic

Thanks you for appreciating it, I will definitely add links to it soon.