Today we are
going to Cover something that I had touched on previously in Day 4. PowerShell and its use of Objects.
Every time
that you run a cmdlet in PowerShell and get output to your screen you are
seeing text. But under the hood PowerShell is actually creating objects (unless otherwise told). There
is a ton of information that you cannot see immediately, but if you know where
and how to look there is a wealth of information. For example, as I stated
previously running the Get-Process command only displays 8 or so pieces of
information but if you export it to a CSV or HTML file you will see that there
are 60+ columns of information that are actually within the output.
Don explains
the way PowerShell is actually handling this data better than anyone for me to
date. What you are seeing as a table when its displayed is actually made up of
four things. To make this easier to visualize lets export our process list to
CSV file. Open your PowerShell windows and type:
Get-Process | Export-CSV <path>\<filename>
Open the CSV
file you just created and follow along. What PowerShell is actually showing is:
1.
Objects:
Each row of the CSV file is actually an individual object.
2.
Properties:
These are the columns. Each column header has a name and these are the
properties of that object.
3.
Methods:
These are not actually shown. Methods are the actions that can be taken against
any or all of the objects.
4.
Collection:
This is basically the entire table. It is the sum of all the objects, or rows,
there associated properties, or columns, and the methods, or actions, that can
be taken against them.
PowerShell creates
and uses these objects for a couple of reasons. The first of which Don points
out is that Windows is an object oriented operating system, and most of its
programs are object oriented. Windows being object oriented makes it extremely
easy for PowerShell to use these objects and create its own. The Second reason is for ease of use. Using
objects makes parsing the data infinitely easier.
Ok so you don’t
want to have to output everything to CSV or HTML to find out what the
properties and methods are do you? No, of course not. Well this is where Get-Member comes in. You can pipe nearly
every command to Get-Member, or its
alias GM, cmdlet and it will display all the associated
properties and methods. There are actually several different kinds of
properties (ScriptProperty, Property, NoteProperty, and AliasProperty) but these
are not important right now. They are all just properties for now. Let's take a
look at them.
Get-Process | GM
This will
display all of associated 60+ properties, methods, and events of the collection.
This is important because you can display and sort these properties in whatever
fashion you want with a couple of cmdlets. Select-Object
and Sort-Object.
Say you
wanted to display a list of running process' in order by name, and you wanted
to display only the Name, Memory Usage,
ID, and Start Time. You could run the following command
Get-Process | Select-Object -property name,VM,ID,StartTime |
sort-object -property name
Or you could
shorten it with alias'
Get-Process | Select -prop name,vm,id,starttime | sort -prop name
As you can
see with the Select-Object you can
specify multiple properties by separating them with commas, but do not use
spaces. PowerShell will see that as the start of a new variable and error. This
can be done with any cmdlet that has multiple properties. You could also
specify methods in this manner but the important ones have cmdlets of their own
(method: Kill , cmdlet: Stop-Process).
Just remember
that almost everything in PowerShell is an object that has properties, and they
can be manipulated. Everything is an object until you tell it not to be.
No comments:
Post a Comment