Thursday, March 15, 2012

Month of Lunches - Day 7


So todays lesson is "The Pipeline, Deeper". This topic went a little deeper than I expected and I had to read a lot of it twice. Not sure if that is because I am tired or just do not understand it. Yet! So for those of you who have not been following along the Pipeline or Pipe is how you easily pass the values of one cmdlet in PowerShell to another cmdlet to get further detail or request that something further be done with the data.

The first thing we cover in this chapter is pipeline input ByValue. What this means is you can pipe a value to a cmdlet. Let's look at the example from the book. Now pull up the full help for the Stop-Service cmdlet.

                Help Stop-Service -Full

You will notice that there are three parameter sets for this service. One of these parameter sets contains the -InputObject parameter as mandatory. Now scroll down and look for the -InputObject parameter.

                -InputObject <ServiceController[]>
                Specifies ServiceController objects representing the services to be stopped. Enter a variable that contains
                 the objects, or type a command or expression that gets the objects.

                Required?                               true
                Position?                                                 1
                Default value
                Accept pipeline input?            true (ByValue)
                Accept wildcard characters?                  false

What you should see is that this parameter Accepts Pipeline Input and that it can take that input ByValue. You should also note that this parameter will only take input of a variable that contains objects or obejects piped from a command that gets them.

Ok so what does that mean? This is the way that I understand it. Under the hood when you run something like:

                Get-Process -name BITS | stop-service

PowerShell is getting the BITS service object and sending it to the Stop-Service. Know I know that you can just do Stop-Service BITS but this is an example. Ok so what this means is that you can also do something like:

                "BITS" | Stop-Service

PowerShell is taking "BITS" as a Service Name object and piping that to stop service. You can do more by seperating them with a comma if you would like. Interesting. Now there is a second pipeline input that you may see as well. If you go back to your PowerShell window and look at the -Name parameter you will notice ByPropertyName.

                -Name <String[]>
                Specifies the service names of the services to be stopped. Wildcards are permitted.
                The parameter name is optional. You can use "Name" or its alias, "ServiceName", or you can omit the parameter
                name.

                Required?                               true
                Position?                                                 1
                Default value
                Accept pipeline input?            true (ByPropertyName, ByValue)
                Accept wildcard characters?                  true

ByPropertyName only picks up and attempts to run if ByValue does not.

This Chapter also contained quite a few little Tips and Tricks like Creating multiple user accounts in Active Directory in seconds from PowerShell by using properly formatted CSV files and piping that information to New-ADUser, how you rename and set customer properties in CSV files, and how to use parentheses to specify what part of command runs first to ensure the proper results.

I have a better grasp of the pipeline but there is still so much to learn and understand.

No comments:

Post a Comment