Adding a second parameter in Visual Basic .NET

Painting Data Matrix ECC200 in Visual Basic .NET Adding a second parameter

Listing 21.4 Adding a second parameter
Data Matrix 2d Barcode Maker In Visual Basic .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Data Matrix ECC200 image in .NET framework applications.
www.OnBarcode.com
Data Matrix 2d Barcode Recognizer In Visual Basic .NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
function GetServerInfoWork { param([string]$computername,[string]$logfile) $os = Get-WmiObject Win32_OperatingSystem -computer $computername $disk = Get-WmiObject Win32_LogicalDisk -filter "DeviceID='C:'" ` -computer $computername # use $logfile to get the value from the # -logfile parameter $obj = New-Object -TypeName PSObject
Barcode Encoder In Visual Basic .NET
Using Barcode generator for VS .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
1D Barcode Generation In Visual Basic .NET
Using Barcode creation for .NET framework Control to generate, create 1D image in .NET framework applications.
www.OnBarcode.com
Functions that look like cmdlets
QR Code 2d Barcode Creator In Visual Basic .NET
Using Barcode generator for .NET framework Control to generate, create Denso QR Bar Code image in .NET applications.
www.OnBarcode.com
Barcode Maker In Visual Basic .NET
Using Barcode generator for .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
$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 } function Get-ServerInfo { param ( [string[]]$computername, [string]$logfile ) BEGIN { $usedParameter = $False if ($PSBoundParameters.ContainsKey('computername')) { $usedParameter = $True } } PROCESS { if ($usedParameter) { foreach ($computer in $computername) { GetServerInfoWork -computername $computer ` -logfile $logfile } } else { GetServerInfoWork -computername $_ ` -logfile $logfile } } END {} } #Get-ServerInfo -verbose -computername (Get-Content c:\names.txt) Get-Content c:\names.txt | Get-ServerInfo -logfile test.txt | Format-Table -auto
Generating PDF417 In Visual Basic .NET
Using Barcode printer for .NET Control to generate, create PDF417 image in Visual Studio .NET applications.
www.OnBarcode.com
USPS PLANET Barcode Creation In Visual Basic .NET
Using Barcode generation for .NET framework Control to generate, create USPS PLANET Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
TRY IT NOW You can see that I ve commented out one of the script s final lines,
Data Matrix Scanner In Visual Basic .NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Data Matrix Decoder In C#
Using Barcode reader for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
so that I m only running one test at a time.
EAN 128 Recognizer In Visual C#
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Drawing Code 128B In Java
Using Barcode creation for Java Control to generate, create Code 128C image in Java applications.
www.OnBarcode.com
21.3 Functions that look like cmdlets
Data Matrix 2d Barcode Encoder In .NET
Using Barcode printer for Reporting Service Control to generate, create Data Matrix 2d barcode image in Reporting Service applications.
www.OnBarcode.com
Linear Maker In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create 1D Barcode image in ASP.NET applications.
www.OnBarcode.com
We re coming very close to creating a function that looks and works, for almost all purposes, like a real cmdlet. About the only thing we re missing is declarative pipeline input. In the previous example, I checked to see if the -computerName parameter was used. If it was, I used the parameter, and if it wasn t, I used $_ instead. With declarative
QR Code Generation In None
Using Barcode encoder for Online Control to generate, create QR-Code image in Online applications.
www.OnBarcode.com
DataMatrix Creation In Visual C#
Using Barcode maker for .NET framework Control to generate, create DataMatrix image in .NET applications.
www.OnBarcode.com
Creating your own cmdlets and modules
Code 128A Scanner In VS .NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Scanning GTIN - 12 In Visual C#
Using Barcode reader for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
pipeline input, you can have the shell automatically attach the pipeline input to the -computerName (or whatever) parameter, leaving you one less thing to deal with. At the same time, you can ask the shell to do a lot of parameter input validation, like making sure mandatory parameters are specified. All of this mainly involves messing around with the Param() block to create a more formal kind of parameter declaration a cmdlet binding _style of declaration, to be exact. Finally, you don t have to use PSBoundParameters: your input will always be in the variable defined for the parameter. The PROCESS script block will execute at least once, so you can simplify things a lot. If input comes as a parameter, PROCESS will execute once and you ll need to manually enumerate what s in that parameter (because it might be more than one thing). If input comes from the pipeline, the parameter will only contain one thing at a time, but you can still enumerate it, meaning that you can use the same exact code. This makes the code less complicated, because PowerShell is doing a lot of the hard work under the hood. The result, shown in listing 21.5, is informally called a script cmdlet by the PowerShell community and is formally called an advanced function in PowerShell s documentation. Run help about_functions_advanced* for help topics.
Making ANSI/AIM Code 128 In None
Using Barcode maker for Microsoft Word Control to generate, create USS Code 128 image in Word applications.
www.OnBarcode.com
Encode EAN 13 In Objective-C
Using Barcode creator for iPhone Control to generate, create European Article Number 13 image in iPhone applications.
www.OnBarcode.com
Listing 21.5 Making our filtering function into an advanced function
function GetServerInfoWork { param([string]$computername,[string]$logfile) $os = Get-WmiObject Win32_OperatingSystem -computer $computername $disk = Get-WmiObject Win32_LogicalDisk -filter "DeviceID='C:'" ` -computer $computername # use $logfile to get the value from the # -logfile parameter $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 } CmdletBinding function Get-ServerInfo { directive [CmdletBinding()] param ( [Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
Parameter attributes
Functions that look like cmdlets
[Alias('host')] [string[]]$computername, [string]$logfile
Parameter alias
) BEGIN {} PROCESS { foreach ($computer in $computername) { GetServerInfoWork -computername $computer ` -logfile $logfile } } END {} } #Get-ServerInfo -verbose -computername (Get-Content c:\names.txt) Get-Content c:\names.txt | Get-ServerInfo | Format-Table -auto
The changes here were all made to the Get-ServerInfo public function. I started by adding the [CmdletBinding()] directive B, which tells the shell that I ll be using the extended, cmdlet-style parameter attributes. I didn t add any attributes to the -logfile parameter, but I added three to the -computername parameter, declaring it as mandatory, and indicating that it should accept input from the pipeline both ByValue and ByPropertyName c. That means both of these examples will now work:
Get-ADComputer -filter * | Select @{l='computername';e={$_.name}} | Get-ServerInfo Get-Content names.txt | Get-ServerInfo
You ll also see where I declared an alias for the parameter d, meaning that the eventual user of this function could use -host as well as -computername. In the body of the code, my only change was to remove $_ and use $computername instead. Because the shell now knows that $computername is the target for pipeline input, there s no longer any need to use $_. When input is piped into the function, $computername will contain one object at a time within the PROCESS script block, just as $_ did in the filtering function earlier in this chapter. When input is fed through a parameter, $computername will contain all the objects given to the parameter, so I enumerate them using a ForEach block. You can go a bit further with these functions. For example, earlier in this book you learned that the -confirm and -whatif parameters are supported by most cmdlets that attempt to modify the system. You can add that same kind of support to an advanced function, and PowerShell does most of the work. To illustrate this, let s use a slightly different example, shown in the next listing.
Copyright © OnBarcode.com . All rights reserved.