• No se han encontrado resultados

BIBLIOGRAFIA

In document Minoristas Ventas al (página 109-142)

If you use a Format cmdlet, it needs to be the last command on your com-mand line.

The only exceptions are the Out-File, Out-Printer, and Out-Host cmdlets.

Because Out-Host is the default, you’ll probably never type it, leaving you with just Out-File and Out-Printer. Those are the only commands that can come after a Format cmdlet.

Follow those rules, and you’ll be fine. Don calls this the “Format Right” rule, meaning you want to move your formatting as far to the right—toward the end—of the com-mand line as possible.

9.4.2 Select or format?

You’ve probably noticed that both Select-Object and the Format cmdlets let you specify the properties you want to see (just one for Format-Wide, but multiple proper-ties are accepted by the other two). You’ll also notice that Select-Object, Format-List, and Format-Table all support the same custom property syntax.

So which one do you use? It depends a bit on what you want to do.

If you’ve finished manipulating your objects and you’re ready to display them, it’s probably easier to select properties, and make custom properties, using Format-Table or Format-List. Keep in mind, though, that the output from that point either has to go to the screen, to a plain-text file, or to a printer.

If you haven’t finished manipulating your objects, then use Select-Object.

Keep in mind that it isn’t dealing with formatting, so it doesn’t have the ability to set column widths, alignment, or formatting strings. But the output can be sent on to other cmdlets.

Sometimes you’ll end up using both, or you’ll end up needing to be very clever. For example, suppose you want to query a computer and get information about its local disk drives. You want to display each drive’s drive letter, total size in gigabytes, and free space in gigabytes. You want both of those values to carry two decimal places. You want to display only drives whose free space is less than 20 percent of the total space.

This is like one of those logic puzzles, where you’re told that Jill lives in a red house, Joe lives next door to Jill, and Kim lives in a small house, and then you have to figure out who lives where. Here are the facts:

You can use WMI to query the drive information. WMI is covered in chap-ter 39, so we won’t focus on it now. It works a lot like the other Get cmd-lets you’ve seen.

You need to filter out the drives that you don’t want displayed. This is best done as early as possible in the command line.

You have some formatting requirements (two decimal places) that Format-Table can accommodate, but you need to decide if that’ll be the last thing you want to do with the objects.

Here’s one example of how you could construct the command:

Get-WmiObject -class Win32_LogicalDisk -filter "drivetype=3" | Select-Object @{name='DriveLetter';expression={$_.DeviceID}}, @{name='Size';expression={$_.Size / 1GB}},

@{name='FreeSpace';expression={$_.FreeSpace / 1GB}},

@{name='PercentFree';expression={$_.FreeSpace / $_.Size * 100}} | Where-Object { $_.PercentFree -lt 20 } |

Format-Table DriveLetter,

@{name='Size';FormatString='N2';expression={$_.Size}},

@{name='FreeSpace';FormatString='N2';expression={$_.FreeSpace}} -auto

Let’s walk through this. You start by getting your WMI objects. Easy enough. Then, you use Select-Object to do the math needed to generate values in gigabytes, rather than the default bytes provided by WMI. Then, you filter out the drives you don’t want by using Where-Object. Finally, you need to use Format-Table, because it’s the only thing that can do the formatting necessary to get just two decimal places.

Was this the easiest way? Probably not. Here’s a better approach:

Get-WmiObject -class Win32_LogicalDisk -filter "drivetype=3" | Where-Object { $_.FreeSpace / $_.Size -lt .2 } |

Format-Table @{name='DriveLetter';expression={$_.DeviceID}},

@{name='Size';expression={$_.Size / 1GB};FormatString='N2'},

@{name='FreeSpace';expression={$_.FreeSpace / 1GB};FormatString='N2'} -auto

This example shortens the command line to just three commands. You’re filtering much earlier in the process, which is always good. There’s no reason to convert the val-ues to gigabytes before doing that filtering, because you can do the division operation with bytes just as easily. You don’t need to make a “PercentFree” column at all, because you don’t want that in your output. You only have to go through the ugly custom-property–making syntax once, at the very end, where it can also handle your format-ting instructions. Remember though that all this will do is send pretty output to the screen or a text file, assuming you piped to Out-File. You can’t do anything else with it.

So it’s largely a matter of being careful and clever and spending some time think-ing about what’s happenthink-ing and what you want to achieve.

9.4.3 Format, out, export—which?

By this point in this book, we’ve thrown at you a lot of different ways to get informa-tion in and out of the shell:

Import-CSV and Export-CSV

Get-Content

Out-File

Format-Table

If you’re following along, you’re probably starting to wonder what the difference is.

We certainly see classroom students trying to run stuff like this:

Get-Content data.csv | Where { $_.Column1 –eq 'value' } | Select Column1,Column2,Column3

125 Summary

And that just won’t work. Here’s the deal:

Get-Content and Out-File both work with plain, unstructured text. If you use Get-Content to read in a CSV file, it doesn’t attempt to interpret the file or break apart its contents. It just spews the text onto the screen, including the col-umn headers. You can’t treat that as data objects. Out-File doesn’t do any con-version of the data; what appears in the file is exactly what would’ve otherwise appeared on the screen.

Import-CSV and Export-CSV deal with structured data. Export-CSV takes objects, which is one kind of data structure, and transforms them into CSV, which is another kind of data structure. Import-CSV does the opposite. If you want to be able to work with columns as properties, you use these.

Format cmdlets discard your data structure and instead create a visual display that’s only meaningful to humans. There’s no practical way to bring that infor-mation back into the shell in any usable form.

We know, it’s a lot to keep track of, but that’s the price of being a PowerShell guru!

9.5 Summary

Formatting is one of the great powers of PowerShell. With a bit of planning and clev-erness, you can produce incredible-looking reports, send them out to files or to a printer, or just look at them onscreen—all without much effort. There are some cave-ats and gotchas involved, and we’ve tried to cover those thoroughly for you here. Prac-tice is the best thing for formatting, so jump in and start seeing what you can do.

Part 2

In document Minoristas Ventas al (página 109-142)