c# barcode zebra printer Creating your own cmdlets and modules in VB.NET

Generator ECC200 in VB.NET Creating your own cmdlets and modules

Creating your own cmdlets and modules
Data Matrix ECC200 Encoder In VB.NET
Using Barcode generator for VS .NET Control to generate, create Data Matrix ECC200 image in VS .NET applications.
www.OnBarcode.com
ECC200 Recognizer In Visual Basic .NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
At the end of chapter 19, you saw how to make a function (in listing 19.5) that output custom objects to the pipeline. Mastering that kind of output is a key to becoming a PowerShell guru, but there s also the question of input. In chapter 19, we passed input to the function by means of a parameter. In this chapter, we re going to look at some other means of getting input into the function. By combining different input techniques with what you already know about producing output, you ll find that you can create a tool that behaves almost exactly like a PowerShell cmdlet!
Print 2D Barcode In VB.NET
Using Barcode creation for VS .NET Control to generate, create 2D Barcode image in .NET applications.
www.OnBarcode.com
Generating GTIN - 13 In Visual Basic .NET
Using Barcode drawer for .NET framework Control to generate, create UPC - 13 image in .NET applications.
www.OnBarcode.com
21.1 Turning a reusable tool into a full-fledged cmdlet
QR Code JIS X 0510 Creator In VB.NET
Using Barcode printer for VS .NET Control to generate, create Quick Response Code image in .NET applications.
www.OnBarcode.com
UCC.EAN - 128 Generation In Visual Basic .NET
Using Barcode creation for Visual Studio .NET Control to generate, create GS1 128 image in .NET framework applications.
www.OnBarcode.com
As I said, the function in listing 19.5 accepted input primarily through a parameter. In order to make a tool like that more useful, it would be nice if we could pass in multiple pieces of input (the function in listing 19.5 only worked with a single computer name, for example), and pass them in either using a parameter or from the pipeline. That would give us a fully reusable tool that looks and works much like a cmdlet. Ideally, we could even have the shell do some input validation for us, such as marking a parameter as mandatory and automatically prompting the user if it wasn t provided. To get you to that point, I m going to take a slightly roundabout path. There are three broad kinds of functions you can write in the shell. 19 covered one of them a simple parameterized function (I guess a non-parameterized function could be considered to be an even simpler, fourth type, but I don t write many of those myself). I ll start by showing you a second type, which accepts pipeline input instead.
Generating PDF417 In Visual Basic .NET
Using Barcode generator for .NET framework Control to generate, create PDF-417 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Create UPC - E0 In Visual Basic .NET
Using Barcode creation for .NET Control to generate, create GTIN - 12 image in Visual Studio .NET applications.
www.OnBarcode.com
Functions that work in the pipeline
Data Matrix 2d Barcode Drawer In None
Using Barcode generation for Office Word Control to generate, create DataMatrix image in Microsoft Word applications.
www.OnBarcode.com
Data Matrix Generator In .NET Framework
Using Barcode generation for Reporting Service Control to generate, create Data Matrix 2d barcode image in Reporting Service applications.
www.OnBarcode.com
21.2 Functions that work in the pipeline
Encode UCC-128 In .NET
Using Barcode printer for .NET Control to generate, create UCC-128 image in .NET applications.
www.OnBarcode.com
Drawing Code 39 Extended In Visual Studio .NET
Using Barcode creation for ASP.NET Control to generate, create Code39 image in ASP.NET applications.
www.OnBarcode.com
The next type of function I ll introduce you to is called a pipeline function, or filtering function. If a regular parameterized function is distinguished by its ability to accept input only through parameters, then a filtering function has these distinguishing characteristics:
Generate EAN13 In Visual C#
Using Barcode encoder for .NET framework Control to generate, create EAN13 image in .NET applications.
www.OnBarcode.com
Barcode Scanner In Visual Studio .NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
You can accept one kind of information through the pipeline. This might be computer names, processes, or any other single kind of information. Whatever you accept through the pipeline can come as a single object, or multiple objects can be piped in. You ll write one (or many) commands that execute against each piped-in object, no matter how many there are. You can designate additional parameters for other input elements. The values provided to these parameters will be used for each execution of your commands.
Barcode Drawer In .NET
Using Barcode drawer for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
QR Code 2d Barcode Scanner In .NET
Using Barcode recognizer for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
That ll all probably make more sense with an example. We ll start with the same function in listing 19.5, but I ll dress it up slightly to make it a filtering function, shown next.
Paint PDF417 In VS .NET
Using Barcode maker for Reporting Service Control to generate, create PDF 417 image in Reporting Service applications.
www.OnBarcode.com
Encode DataMatrix In Java
Using Barcode generation for Android Control to generate, create Data Matrix image in Android applications.
www.OnBarcode.com
Listing 21.1 A filtering function
Generating Barcode In None
Using Barcode generator for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Code128 Drawer In None
Using Barcode maker for Online Control to generate, create Code128 image in Online applications.
www.OnBarcode.com
function Get-ServerInfo { BEGIN {} PROCESS { $computername = $_
Runs first
Defines PROCESS block
Uses $_ placeholder
$os = Get-WmiObject Win32_OperatingSystem -computer $computername $disk = Get-WmiObject Win32_LogicalDisk -filter "DeviceID='C:'" ` -computer $computername $obj = New-Object -TypeName PSObject $obj | Add-Member -MemberType NoteProperty ` -Name ComputerName -Value $computername $obj | Add-Member -MemberType NoteProperty ` -Name BuildNumber -Value ($os.BuildNumber) $obj | Add-Member -MemberType NoteProperty ` -Name SPVersion -Value ($os.ServicePackMajorVersion) $obj | Add-Member -MemberType NoteProperty ` -Name SysDriveFree -Value ($disk.free / 1MB -as [int]) Write-Output $obj } END {} } Get-Content names.txt | Get-ServerInfo | Format-Table -auto
Runs last
Creating your own cmdlets and modules
This function isn t terribly different from the original one (refer back to listing 19.5 to see that one). I ve added a BEGIN block B and, at the end of the function, an END block e. Whatever s inside of the BEGIN block will execute the first time this function is called in the pipeline; the END block will execute when the function is almost finished. As you can see, I don t put any code in these, so nothing will happen during those two stages. I could omit BEGIN and END entirely, but I like to include them to keep the structure consistent across all of my functions. The PROCESS script block is where the magic happens c. This block will execute one time for each object that s piped into the function (if you don t pipe in any input, the PROCESS block will execute once). This script expects computer names to be piped in, so if you pipe in four names, the PROCESS block will run four times. Each time, the $_ placeholder d will be automatically populated with a new object from the pipeline. But rather than utilizing $_ directly, I ve copied its object into the $computername variable. Doing so has two advantages: First, my commands were already using $computername, so continuing to use it means less work for me. Second, the variable name is clearer than $_, making it easier for me to keep track of what the variable is supposed to contain. There s a third, overlooked advantage: $_ will be repopulated if an error occurs, so by copying it to $computername now, I won t lose the initial value. The last line of the script shows how you would execute this function: pipe a bunch of string objects to it. So long as those objects are computer names, everything should work fine. Another way to execute it would be this:
Copyright © OnBarcode.com . All rights reserved.