- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Adding logic and loops in Visual Basic .NET
Adding logic and loops Data Matrix 2d Barcode Encoder In VB.NET Using Barcode generator for Visual Studio .NET Control to generate, create DataMatrix image in .NET framework applications. www.OnBarcode.comScan Data Matrix ECC200 In Visual Basic .NET Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET applications. www.OnBarcode.comForEach may seem familiar to you, because we ve used the ForEach-Object cmdlet (and perhaps you ve used that cmdlet s foreach alias) in previous chapters. The construct works much the same as the cmdlet: both go through a collection of objects one at a time. With the construct, you get to define the variable that contains one object at a time (I used $service in the preceding example); with the cmdlet, PowerShell forces you to use the $_ placeholder. For example, I could rewrite the previous example like this: Create UCC - 12 In Visual Basic .NET Using Barcode creation for .NET Control to generate, create UCC-128 image in .NET framework applications. www.OnBarcode.comBarcode Maker In VB.NET Using Barcode drawer for Visual Studio .NET Control to generate, create Barcode image in VS .NET applications. www.OnBarcode.comGet-Service | ForEach-Object { Write-Host $_.Name } UPC Symbol Encoder In Visual Basic .NET Using Barcode drawer for VS .NET Control to generate, create UPCA image in .NET framework applications. www.OnBarcode.comDrawing Code 128B In VB.NET Using Barcode creator for .NET Control to generate, create Code 128A image in Visual Studio .NET applications. www.OnBarcode.comWhen you use the construct, you use the in keyword, as I did in my ForEach example. When you use the cmdlet, you don t need to use the in keyword because PowerShell automatically enumerates into the built-in $_ placeholder. Generating ECC200 In VB.NET Using Barcode generation for .NET framework Control to generate, create Data Matrix ECC200 image in .NET framework applications. www.OnBarcode.comMSI Plessey Generator In Visual Basic .NET Using Barcode encoder for .NET Control to generate, create MSI Plessey image in .NET applications. www.OnBarcode.com20.7 Why scripting isn t always necessary
Data Matrix ECC200 Encoder In Objective-C Using Barcode generation for iPad Control to generate, create Data Matrix image in iPad applications. www.OnBarcode.comData Matrix Creator In VS .NET Using Barcode printer for Reporting Service Control to generate, create Data Matrix 2d barcode image in Reporting Service applications. www.OnBarcode.comForEach is an excellent example of why scripting like this isn t always necessary in Pow- Generate Universal Product Code Version A In C# Using Barcode generation for .NET Control to generate, create GTIN - 12 image in .NET applications. www.OnBarcode.comGenerate PDF417 In Java Using Barcode generation for Java Control to generate, create PDF 417 image in Java applications. www.OnBarcode.comerShell, even though PowerShell will let you do it. That previous example could have been accomplished more easily in the pipeline: Barcode Decoder In Java Using Barcode Control SDK for BIRT reports Control to generate, create, read, scan barcode image in Eclipse BIRT applications. www.OnBarcode.com1D Barcode Maker In Java Using Barcode maker for Java Control to generate, create Linear Barcode image in Java applications. www.OnBarcode.comGet-Service | Select Name
Scanning Barcode In Java Using Barcode scanner for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comUPC Symbol Creator In Visual Studio .NET Using Barcode creator for ASP.NET Control to generate, create UPC-A Supplement 2 image in ASP.NET applications. www.OnBarcode.comThat will produce new objects that have only a Name property. If you wanted to get the actual names as simple string values, you could do this: Data Matrix 2d Barcode Creator In Java Using Barcode creator for Android Control to generate, create Data Matrix ECC200 image in Android applications. www.OnBarcode.comBarcode Scanner In Java Using Barcode scanner for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comGet-Service | Select -expand Name
Draw ECC200 In None Using Barcode encoder for Word Control to generate, create Data Matrix 2d barcode image in Office Word applications. www.OnBarcode.comUPC-A Supplement 5 Drawer In .NET Using Barcode creation for VS .NET Control to generate, create UPC-A Supplement 5 image in Visual Studio .NET applications. www.OnBarcode.comBoth of those options involve a lot less typing. The point here is that using ForEach is often (but not always) an indicator that you re taking a scripting approach rather than a pure PowerShell approach. You won t get in trouble for taking a scripting approach, but it often requires a lot more typing and a script than using a couple of commands. There are only two times when I find myself legitimately using ForEach: When there s no cmdlet capable of doing what I need to a bunch of objects at once. This most often happens when I need to execute a method against a bunch of objects, and there isn t a cmdlet that can perform the equivalent task. When I need to manually unwind a bunch of objects and send them off, one at a time, to a custom function that I ve written, which can only work with one at a time. You ll see an example of this in the next chapter. If you have some scripting or programming in your background, and you want to try to force yourself to take a more pure PowerShell approach, use ForEach as a cue. When you find yourself using it, see if there isn t an easier way. For example, instead of this, $processes = Get-Process ForEach ($process in $processes) { If ($process.name -eq 'notepad') { $process.kill() } } you could just do this, Get-Process | Where-Object { $_.Name -eq 'notepad' } | Stop-Process
or better yet, this, Get-Process -name notepad | Stop-Process
or best of all, this: Stop-Process -name notepad
These all accomplish the same thing, but the command-oriented way takes a lot less typing (and to me, is easier to read and figure out) than the scripting-oriented way. 20.8 Lab
For this lab, you ll probably want to work within the PowerShell ISE. That will make it easier to enter multiline scripts and commands, and it ll make it easier to edit if you make any mistakes or want to make a change. Create a script that uses Read-Host to prompt for a remote computer name. If the computer name is localhost, then don t do anything. Otherwise, query the Win32_OperatingSystem WMI class from the specified computer. Create a script that queries the Win32_LogicalDisk WMI class from the local computer. Use a ForEach loop to enumerate the instances returned by the query. Within the ForEach loop, display the DeviceID property. Then display a text description of the DriveType property by using a Switch construct. For example, if the DriveType is 3, display Fixed Disk. Use a search engine to search for Win32_LogicalDisk, and you ll locate the documentation page for that WMI class. The documentation page will display the possible values, and meanings, of the DriveType property.
|
|