DAY-6: OBJECT'S IN POWERSHELL














Objects: Data By Another Name

Use of Objects in PowerShell sometimes can be one of the most confusing elements but at the same time, it's the also one of the most critical concepts, affecting everything you do in the shell.

What are the Objects?

Run a command Get-Process in PowerShell. You Should see a table with several columns, but those columns barely have the complete information about the processes. Each process object also has a machine name, main window handle, maximum working set, exit code and time, and a great deal of information. In fact, you'll find more than 60 pieces of information associated with a process. Now the question is, why did PowerShell show so few of them?

The simple fact is that most of the things PowerShell can access offer more information than what will comfortably fit on the screen. When you run a command, such as Get-Process, Get-Service, or anything, PowerShell constructs a table containing all the information in system memory. In the case of Get-Process, that table consists of 67 Column with one row for each process that's running on your system. Then, PowerShell, try to look, what information you have specified, if you have not specified any, then shell looks for configuration provide by Microsoft and display only those column.

One way to see all the columns is by running below command,

Get-Process | Convertto-HTML | Out-File process.html 

This Cmdlet doesn't bother filtering the down the column and output an HTML file containing all the columns.

Some terminologies, related to PowerShell objects,

1. Object- The entire row in the memory table, it represents Object. It represents a single thing, like a single process or a single service

2. Property- This is what we call "table column." It represents one piece of information about an object, like a process name, process ID.

3. Method - This is what we called an "action." A method is related to a single object and makes the object do something, like killing a process or starting a service.

4. Collection- This is the entire set of objects, or what we've been calling a table.

Why PowerShell Uses Objects?

The first reason is that Windows itself is an object-oriented operating system. Most of the software that runs on Windows is object oriented. Another reason to use objects is that they ultimately make things easier on you and give you more power and flexibility.

For a moment, we can think PowerShell produces simple text tables. When you run a command like Get-Process, you're getting formatted text output. From the text table, the commands for filtering and getting a single column from the row is a very tricky task. This task is very simplified in object-oriented shells.

Discovering Objects

If the object like a giant table in memory, and PowerShell only shows you a portion of that table on the screen. Then how you can see what else you have to work with? Get-Member is the answer to this question. Try running the command like in the below Figure.















All the properties, method and other things to an object are collectively called its Members. THat's where Get-Member Cmdlet takes its name from: it's getting a list of objects' members. This types of MemberType you will find when you run this command. I have tried it to explain what actually MemberType resembles.

1. AliasProperty- This simply denotes to the Alias of a Parameter that Cmdlets contain. From the below text, we can see that Name is the Aliasproperty of the Parameter ServiceName in Cmdlet Get-Service.

   Name                      MemberType                    Definition
     ----                            ----------                         ----------
   Name                      AliasProperty                   Name = ServiceName

If you run the below commands in the figure, you will find both shows the same result.
















 2. Methods- This is defined as the action we can perform using the Cmdlet. Eg. start, stop,       
     continue etc. A service object has a start method, which will start the service.

But the fact is, you may spend years working with PowerShell and never need to execute a single object method. That's because many of that action can be performed by Cmdlets provide in PowerShell,

Let take an example of starting a starting a Service. We have 3 ways to perform the same, by using Cmdlets and by using the method.

Get-Service -Name WinRM | Start-Service

Start-Service -Name WinRM

(Get-Service -Name WinRM).start()                            This is using Method


3. Properties- This is defined by the read-only values that contain a value. For example, the value of process object's ID property might be 123, and the Name property of that object might have a value notepad. properties describe something about the object: its status, its ID, its Name, and so on.

This is all we have in today's blog. We will be covering more on Objects in my next blog. I will continue with the same topic.

1. PowerShell Objects

Comments

Popular posts from this blog

DAY-1 : POWERSHELL…WHAT IT IS>>>AND WHY

Day-5: PowerShell Modules & PSSnapin

DAY-7: POWERSHELL OBJECT'S