Todays
chapter is "Filtering and Comparison". This is another great chapter
about how to get the most out of your PowerShell viewing experience. We have
covered a little of this in previous chapters just for example of another topic
but now we get to dive right into what it is.
The Very
first thing that Don covers in this chapter is Filter Left. Basically this
means put your filters as far to the left (or as close to the beginning) as
possible. This allows all the biggest part of the work to be done in the beginning
of a command leaving less for the cmdlets at the end to do.
This where we
are introduced to Where-Object or Where (Oddly enough while researching
this I found that this also has another alias, the questions mark, ?). Where-Object allows you to filter any type
of object pretty much however you want, while its in the pipeline. To even
grasp what this command can do you have to read and re-read the help text. To
put the in perspective there are 35 different parameters that can be used in 31
different parameter sets. There are just so many ways to manipulate objects
with this command.
Next we
covered comparison operators. These are the standard ways of comparing two
objects. These are:
-eq
= Direct comparison of two
objects to see if they are similar. This provides a true or false answer
-ne = Not Equal to. This is the inverse of
-eq. It also provides true or false answer.
-gt
and -lt = Greater Than and
Less Than. Just think < and > from math and you get the idea
-ge and -le = Greater than or equal to and Less
than or equal to.
You can also
specify these to be case sensitive with -ceq, -cne, -cgt, -clt, and so on and
so forth. One thing to remember here is that PowerShell uses $False and $True
to represent false and true in a command.
This next
section apparently make a whole lot more sense to people with programming
experience and this will take me some time to fully grasp. Iterative commands.
I understand what they do but don’t fully understand how they work or how to
use them. If you know please explain this to me in laymen's terms.
The last and
most important thing I took from this lesson is the $_ placeholder. This allows one object or string of a data set to
be processed by the cmdlet at a time and not the collection as a whole. This
one probably needs an example and I will use the one from the book but shorten
it.
Get-Service -computername (get-content c:\names.txt) | Where-Object
-filter ($_ -notlike '*svc')
Ok so here is
what is going on in this command. First the parenthetical portion of the command
is run (get-content portion) and this is ran through the Get-Service
-Computername. This collection are then passed through the pipeline. Now the
collection is passed into the second parenthetical which is a comparison
(-notlike) and then filtered with the Where-Object cmdlet. Makes sense right.
Actually this does make sense to me.
Well that was
it for today. Try some of this out on your own if your new to PowerShell and
even if your not for a basic refresher.
No comments:
Post a Comment