Simple and parameterized functions Listing 19.1 Trying to get all the output into a single table in Visual Basic .NET

Drawer Data Matrix in Visual Basic .NET Simple and parameterized functions Listing 19.1 Trying to get all the output into a single table

Simple and parameterized functions Listing 19.1 Trying to get all the output into a single table
Drawing Data Matrix In VB.NET
Using Barcode generator for VS .NET Control to generate, create Data Matrix ECC200 image in .NET framework applications.
www.OnBarcode.com
Data Matrix Recognizer In VB.NET
Using Barcode reader for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
$computername = 'SERVER-R2'
Encode UPC - 13 In VB.NET
Using Barcode printer for Visual Studio .NET Control to generate, create EAN / UCC - 13 image in .NET applications.
www.OnBarcode.com
Matrix Creator In VB.NET
Using Barcode creation for Visual Studio .NET Control to generate, create Matrix Barcode image in .NET framework applications.
www.OnBarcode.com
Put computer name in a variable
Linear Printer In VB.NET
Using Barcode maker for .NET framework Control to generate, create 1D Barcode image in .NET framework applications.
www.OnBarcode.com
Code128 Generation In Visual Basic .NET
Using Barcode creation for .NET Control to generate, create Code 128A image in .NET applications.
www.OnBarcode.com
$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-Host "Computer`tBuildNumber`tSPVersion`tFreeSpace" Write-Host "========`t===========`t=========`t=========" Write-Host ("{0}`t{1}`t{2}`t{3}" -f ($os.ComputerName), ($os.BuildNumber),($os.servicepackmajorversion), ($disk.sysdrivefree))
Draw Barcode In VB.NET
Using Barcode generator for .NET framework Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
Painting UPC - E0 In VB.NET
Using Barcode creation for .NET Control to generate, create GTIN - 12 image in VS .NET applications.
www.OnBarcode.com
Output table header
Painting Data Matrix 2d Barcode In None
Using Barcode creation for Office Excel Control to generate, create Data Matrix 2d barcode image in Excel applications.
www.OnBarcode.com
Data Matrix 2d Barcode Reader In Visual C#.NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Use -f operator to make a table row
GS1 - 12 Creator In .NET
Using Barcode generation for .NET Control to generate, create UCC - 12 image in Visual Studio .NET applications.
www.OnBarcode.com
Code 128C Printer In VS .NET
Using Barcode encoder for ASP.NET Control to generate, create Code 128 Code Set B image in ASP.NET applications.
www.OnBarcode.com
Okay, that output is pretty awful the columns aren t lining up. But the information we want is there, so let s tackle the formatting later. In listing 19.1, you can see that I ve put the computer name into a variable of its own B. That separates the computer name from the rest of the task. I m also using Write-Host to create a table header c, and then using the -f operator to format a table row d. The string that precedes the -f operator has placeholders like {0} and {1}, as well as tab characters (`t). The comma-separated list after the -f operator provides the values for each of the four placeholders. This will be our starting point.
Decoding Barcode In VB.NET
Using Barcode Control SDK for .NET Control to generate, create, read, scan barcode image in .NET framework applications.
www.OnBarcode.com
Barcode Reader In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
19.3 Simple and parameterized functions
Draw Barcode In None
Using Barcode generation for Office Excel Control to generate, create Barcode image in Office Excel applications.
www.OnBarcode.com
Create UPC-A In C#.NET
Using Barcode generator for .NET Control to generate, create GTIN - 12 image in Visual Studio .NET applications.
www.OnBarcode.com
We want to modularize our code into a function, which is a self-contained, standalone unit of work that we can distribute more easily. An easy way to make a function is to wrap it in a function declaration, as shown here.
Make QR Code JIS X 0510 In Java
Using Barcode encoder for Java Control to generate, create Denso QR Bar Code image in Java applications.
www.OnBarcode.com
Paint Barcode In None
Using Barcode printer for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Listing 19.2 Wrapping the code in a function
PDF417 Recognizer In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Data Matrix 2d Barcode Decoder In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
function Get-ServerInfo { $computername = 'SERVER-R2' $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-Host "ComputerName`tBuildNumber`tSPVersion`tFreeSpace" Write-Host "============`t===========`t=========`t=========" Write-Host ("{0}`t{1}`t{2}`t{3}" -f ($os.ComputerName), ($os.BuildNumber),($os.servicepackmajorversion), ($disk.sysdrivefree)) }
From command to script to function
TRY IT NOW You should be able to enter this function into a blank script
within the PowerShell ISE. To run the function, just add Get-ServerInfo as the last line in the script, underneath the function, and then run the script using the Run toolbar icon (or by pressing F5). That s the same pattern you ll use throughout this chapter: define the function first, and then call the function at the end of the script file. I ve selected a function name that looks like a cmdlet name, using the verb-noun naming convention of a cmdlet. Apart from that, I didn t change anything. We need to change something, though, because we don t want the computer name to always be SERVER-R2. The solution is to change $computername from a variable into a parameter, which we can do by adding it to a Param() block at the top of the function. The next listing shows this new version of the function.
Listing 19.3 Parameterizing the function
function Get-ServerInfo { param ( $computername = 'localhost' )
Variable changed to parameter
$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-Host "ComputerName`tBuildNumber`tSPVersion`tFreeSpace" Write-Host "============`t===========`t=========`t=========" Write-Host ("{0}`t{1}`t{2}`t{3}" -f ($os.ComputerName), ($os.BuildNumber),($os.servicepackmajorversion), ($disk.sysdrivefree)) }
You ll notice that I set $computername equal to localhost. That will now serve as a default value. If someone runs this function without specifying a computer name, the function will target localhost, which is usually a safe operation. The function can be given an alternative computer name in any of these ways:
Get-ServerInfo -computername SERVER-R2 Get-ServerInfo -comp SERVER27 Get-ServerInfo WESTDC4
TRY IT NOW To try any of these examples, add the command to the end of the
script file that contains the function. I suggest continuing to work in the PowerShell ISE so that you can follow this pattern throughout the chapter. In these examples, I used the full parameter name, an abbreviated parameter name, and a positional parameter. All parameters in a Param() block are positional by default,
Returning a value from a function
meaning that you can pass in values in the order in which the parameters are declared, without specifying the parameter names. By the way, if you wanted to have a second parameter, you would just separate it from the first one with a comma. There s also a neat trick that allows the default value to prompt the user. That way, if someone runs the function and doesn t specify the parameter, they re prompted for it. That would look something like this:
Function Test-This { Param( $computername = (Read-Host "Enter computer name"), $logerrors = $True, $logfile ) }
With the parameter in place, we ve completely separated our main functionality from the task of getting computer names.
Copyright © OnBarcode.com . All rights reserved.