Pipeline input ByValue, or why Stop-Service works in VB.NET

Generate Data Matrix ECC200 in VB.NET Pipeline input ByValue, or why Stop-Service works

Pipeline input ByValue, or why Stop-Service works
Encoding Data Matrix In Visual Basic .NET
Using Barcode creation for .NET Control to generate, create Data Matrix 2d barcode image in VS .NET applications.
www.OnBarcode.com
Data Matrix ECC200 Decoder In Visual Basic .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
accept that input ByValue from the pipeline. In this instance, PowerShell discovers that the -InputObject parameter of Stop-Service is willing to accept values of the type ServiceController, from the pipeline, ByValue. So the ServiceController objects generated by Get-Service are passed to the -InputObject parameter of Stop-Service, which uses those to identify the services we want stopped. So when we say that a parameter accepts pipeline input ByValue, we re really saying the parameter will accept incoming objects from the pipeline, so long as those objects match up to the type of value the parameter is designed to accept. Frankly, I found all that to be a bit confusing the first time someone explained it to me, so let s walk through a few more examples. Start by explaining to yourself why this works:
Create USS Code 128 In VB.NET
Using Barcode drawer for .NET framework Control to generate, create ANSI/AIM Code 128 image in Visual Studio .NET applications.
www.OnBarcode.com
Denso QR Bar Code Creation In Visual Basic .NET
Using Barcode generator for .NET framework Control to generate, create Denso QR Bar Code image in VS .NET applications.
www.OnBarcode.com
"BITS","MSISCSI" | Start-Service
Barcode Printer In Visual Basic .NET
Using Barcode maker for .NET framework Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
Paint ECC200 In Visual Basic .NET
Using Barcode printer for .NET Control to generate, create Data Matrix image in .NET framework applications.
www.OnBarcode.com
Here s how I walk myself through the explanation:
Print Barcode In VB.NET
Using Barcode printer for .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
ISSN Generation In VB.NET
Using Barcode creator for .NET Control to generate, create International Standard Serial Number image in Visual Studio .NET applications.
www.OnBarcode.com
I didn t run a command to put objects in the pipeline. Instead, I manually typed some strings and piped them to the next cmdlet. So the type of object in the pipeline is String. Running Help Start-Service -full, I see that the -InputObject and -Name parameters accept pipeline input ByValue. So my strings will attach to one of those two parameters. Of those two, only the -Name parameter accepts String values, so my strings will attach to -Name. Therefore, Start-Service will assume that the strings I ve piped in are service names, and it will try to start services having those names.
Data Matrix ECC200 Creation In Java
Using Barcode encoder for Java Control to generate, create Data Matrix ECC200 image in Java applications.
www.OnBarcode.com
Draw DataMatrix In Objective-C
Using Barcode generator for iPad Control to generate, create Data Matrix image in iPad applications.
www.OnBarcode.com
Next, see if you can figure out whether or not this will work (don t actually run the command just see if you can figure out the explanation):
PDF 417 Scanner In Visual C#.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Make UPC - 13 In Visual Studio .NET
Using Barcode maker for Reporting Service Control to generate, create EAN13 image in Reporting Service applications.
www.OnBarcode.com
Get-Process -name b* | Stop-Service
UPC-A Supplement 5 Recognizer In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
USS-128 Printer In None
Using Barcode drawer for Microsoft Excel Control to generate, create EAN / UCC - 14 image in Excel applications.
www.OnBarcode.com
Again, here s the explanation I would come up with:
Scanning QR-Code In C#
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Code 128 Generator In Objective-C
Using Barcode creator for iPad Control to generate, create Code 128B image in iPad applications.
www.OnBarcode.com
Get-Process is putting something into the pipeline. I would run Get-Process | Gm to discover that the objects generated by Get-Process are of the type Process (technically, System.Diagnostics.Process). Looking at the full help for Stop-Service, I see that both -Name and -InputObject are capable of accepting pipeline input by value. Neither -Name nor -InputObject accept values of the type Process, so I would conclude that the preceding command wouldn t work, because Stop-Service
Paint Code39 In Java
Using Barcode generation for Java Control to generate, create USS Code 39 image in Java applications.
www.OnBarcode.com
Printing Code 128 In None
Using Barcode drawer for Software Control to generate, create Code 128 Code Set A image in Software applications.
www.OnBarcode.com
has no way of accepting the piped-in objects. That conclusion is correct for as far as we ve gotten in this chapter, although we re going to revisit that example later. You ll find that it actually does do something, although it might not be what you want. Here s one final example for you to try to explain:
Encode USS Code 128 In Objective-C
Using Barcode encoder for iPhone Control to generate, create Code-128 image in iPhone applications.
www.OnBarcode.com
Generating ECC200 In Java
Using Barcode drawer for Android Control to generate, create DataMatrix image in Android applications.
www.OnBarcode.com
"conhost" | Stop-Process
The pipeline, deeper
And here s my explanation:
I put an object of type String into the pipeline. You could confirm that by running "conhost" | Gm. I see that Stop-Process has only one parameter that accepts pipeline input ByValue, and that s -InputObject. -InputObject accepts objects of type Process, and not of type String, so I conclude that this command will not work.
That turns out to be a correct conclusion.
Parentheses instead of pipelines
The pipeline is only one way to get information into a parameter. You can manually type a simple value like a name or an ID number. But as you ve seen in some earlier examples, you can also take the output of one cmdlet and send that output to the parameter of another cmdlet without using the pipeline. For example, take a look at the full help for Get-Service, and specifically at the help for its -computerName parameter. You ll notice that this parameter accepts pipeline input, but it doesn t do so ByValue. That means I could not pipe in a list of computer names like this:
Get-Content c:\names.txt | Get-Service
If I ran that command, here s what would happen:
Get-Content puts objects of type String into the pipeline. I can confirm that by running Get-Content c:\names.txt | Gm (the assumption is that Names.txt
contains a list of computer names, with one name per line). I see that Get-Service can accept pipeline input ByValue for its -InputObject and -Name parameters. Of these, -Name accepts values of type String. Get-Service will accept what is in Names.txt as service names and will try to retrieve those services. It won t treat the names as computer names, which was my intent. So the command will run, but I won t get the results I wanted.
That doesn t mean I can t do what I wanted, but it does mean I can t provide the input to the -computerName parameter through the pipeline. Instead, I can use parentheses. Just like in math class, parentheses change the order in which execution occurs. In the case of math, it s the order in which mathematical expressions are evaluated. In the case of PowerShell, parentheses force the shell to execute certain commands before others. I already know that the -computerName parameter of Get-Service can accept multiple string values, because the cmdlet s help lists this:
Copyright © OnBarcode.com . All rights reserved.