Returning a value from a function in VB.NET

Encoder DataMatrix in VB.NET Returning a value from a function

19.4 Returning a value from a function
Making ECC200 In Visual Basic .NET
Using Barcode generator for .NET framework Control to generate, create Data Matrix 2d barcode image in .NET framework applications.
www.OnBarcode.com
ECC200 Reader In Visual Basic .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Now we need to work on the output of our function a bit, because right now it s pretty ugly. First, though, I want to briefly ignore the function we ve been working on and show you one way to output a single value from a function. This is useful in cases where you only need a single value. Here s how you would write the function:
Encoding DataMatrix In Visual Basic .NET
Using Barcode printer for .NET framework Control to generate, create Data Matrix 2d barcode image in VS .NET applications.
www.OnBarcode.com
Draw UPC Code In VB.NET
Using Barcode generator for Visual Studio .NET Control to generate, create Universal Product Code version A image in VS .NET applications.
www.OnBarcode.com
function Get-SPVersion { param ($computername) $os = Get-WmiObject Win32_OperatingSystem -comp $computername return ($os.servicepackmajorversion) }
Create Barcode In Visual Basic .NET
Using Barcode creation for Visual Studio .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
Linear 1D Barcode Creator In VB.NET
Using Barcode encoder for .NET Control to generate, create 1D Barcode image in .NET framework applications.
www.OnBarcode.com
You would run the function, and see the result, as follows:
Barcode Maker In Visual Basic .NET
Using Barcode creator for .NET framework Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
EAN / UCC - 8 Generation In Visual Basic .NET
Using Barcode drawer for VS .NET Control to generate, create European Article Number 8 image in .NET framework applications.
www.OnBarcode.com
PS C:\> Get-SPVersion server-r2 0
DataMatrix Encoder In None
Using Barcode printer for Software Control to generate, create Data Matrix image in Software applications.
www.OnBarcode.com
ECC200 Creation In Objective-C
Using Barcode printer for iPhone Control to generate, create Data Matrix ECC200 image in iPhone applications.
www.OnBarcode.com
You could also capture the function s output into a variable:
ANSI/AIM Code 39 Maker In Objective-C
Using Barcode printer for iPhone Control to generate, create USS Code 39 image in iPhone applications.
www.OnBarcode.com
PDF 417 Recognizer In Visual C#
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
PS C:\> $version = Get-SPVersion server-r2 PS C:\> $version 0
GS1 - 13 Recognizer In Visual C#
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Making Code 128C In .NET Framework
Using Barcode drawer for ASP.NET Control to generate, create ANSI/AIM Code 128 image in ASP.NET applications.
www.OnBarcode.com
The return keyword places a single object (such as an integer, in this example) into the pipeline, and then immediately exits the function. Any code following the return keyword won t ever execute. Our function, however, needs to return more than a single value. It is actually outputting four pieces of information, and we want that information in a table. That means the return keyword isn t suitable. Instead, we could continue using WriteHost, but it doesn t place anything into the pipeline. That means our function could never pipe its output to another cmdlet like this:
Create Code 128B In None
Using Barcode printer for Microsoft Excel Control to generate, create Code 128 Code Set B image in Microsoft Excel applications.
www.OnBarcode.com
Barcode Encoder In Objective-C
Using Barcode printer for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
PS C:\> Get-ServerInfo | Export-CSV info.csv
QR Code JIS X 0510 Generator In None
Using Barcode creation for Online Control to generate, create Denso QR Bar Code image in Online applications.
www.OnBarcode.com
PDF 417 Generator In None
Using Barcode creator for Microsoft Excel Control to generate, create PDF-417 2d barcode image in Office Excel applications.
www.OnBarcode.com
From command to script to function
Code 128C Encoder In Java
Using Barcode maker for Java Control to generate, create Code128 image in Java applications.
www.OnBarcode.com
Make Code 3/9 In Java
Using Barcode creation for Java Control to generate, create Code 39 image in Java applications.
www.OnBarcode.com
In order to pipe our output to another cmdlet, we have to place our output into the pipeline, rather than writing it directly to the screen. As you learned in chapter 16, Write-Output is the way to do that. This listing shows that modification.
Listing 19.4 Using Write-Output to write to the pipeline
function Get-ServerInfo { param ( $computername = 'localhost' ) $os = Get-WmiObject Win32_OperatingSystem -computer $computername | Select @{l='ComputerName';e={$_.__SERVER}}, BuildNumber,ServicePackMajorVersion $disk = Get-WmiObject Win32_LogicalDisk -filter "DeviceID='C:'" ` -computer $computername | Select @{l='SysDriveFree';e={$_.FreeSpace / 1MB -as [int]}} Write-Output "ComputerName`tBuildNumber`tSPVersion`tFreeSpace" Write-Output "============`t===========`t=========`t=========" Write-Output ("{0}`t{1}`t{2}`t{3}" -f ($os.ComputerName), ($os.BuildNumber),($os.servicepackmajorversion), ($disk.sysdrivefree)) }
All I ve done is swap out Write-Output for Write-Host. Running this function, I get the same output that we did before, which I m still not happy with. It looks like this:
Get-ServerInfo ComputerName Build ============ ===== SERVER-R2 7600 Number ====== 0 SPVersion ========= 50077 FreeSpace =========
But let s try piping the output to a CSV file. Run Get-ServerInfo | Export-CSV info.csv, and then open the CSV file in Notepad. This is what I see:
#TYPE System.String "Length" "44" "44" "22"
Not what I wanted at all. This isn t going well. I think we need to consider an entirely different way of producing our output.
19.5 Returning objects from a function
Here s a little-known secret about PowerShell: it doesn t really like text it likes objects. What we ve been doing so far in our function is attempting to format our output as a text table, all on our own. By doing so, we re working against PowerShell s native capabilities, which are making it harder to get the output we want. We need to stop fighting
Returning objects from a function
the shell and instead work with it. That means we need to stop trying to output text, and instead output objects. Because we have information from two places Win32_OperatingSystem and Win32_LogicalDisk we can t directly output either of the objects we got back from WMI. Instead, we need to create a brand-new, blank object that we can use to combine our four pieces of information. PowerShell provides a blank object type called a PSObject for exactly this purpose. We simply need to create one of these, and then add our information to it in the form of properties. Specifically, we ll add our information in the form of a NoteProperty, which is a static piece of information. I m going to make several changes to our function, as shown in listing 19.5. The good news is that the output is exactly what we want:
ComputerName BuildNumber SPVersion SysDriveFree ------------ ----------- --------- -----------localhost 7600 0 0
Copyright © OnBarcode.com . All rights reserved.