Transcript
Page 1: Deep dive formatting

How Do They Do That?Formatting with PowerShell

Thomas Lee

Page 2: Deep dive formatting

Agenda

• Review the basics• Composite Formatting and .ToString• Formatting with Format-tables• Using Format XML• EasyOutput (thanks James!)

• DEMOS!!

Page 3: Deep dive formatting

Who Am I

• Consultant/writer/trainer – living in the UK• Long time PowerShell MVP• 2 blogs– http://tfl09.blogspot.com – Under The Stairs– http://pshscripts.blogspot.com – PowerShell

Scripts Blog

Page 4: Deep dive formatting

Formatting by Default

• PowerShell formats output by default– controlled by *.PS1XML files– Some default rules if all else fails

• How Default Formatting Works– Objects in the pipeline - send to Out-Default– Out-Default looks at the objects in the pipeline– If formatting instructions – send to Out-Host– If objects - send to Format-List or Format-Table

Page 5: Deep dive formatting

Formatting Rules

• If format.ps1xml defines format for a type– Use that

• If not, does type.ps1xml define a default property set– Use that and the rules below

• Otherwise table vs list rules– 4 or less properties, output a table– 5 or more output a list

Page 6: Deep dive formatting

Format-Table and Format-List

Format-List/Format-Table take objects from pipeline Formats those objects

Default contents and ‘shape’ from .ps1xml You can override default properties

GWMI win32_ComputerSystem | fl name,model

Can also take a hash table for precise format control

Page 7: Deep dive formatting

Occurrence Type Drives Formatting

• An object’s type determine how PowerShell renders instances

• .PS1XML defines the format for many types– Others use the last resort rules (1-4/5+)

Page 8: Deep dive formatting

Other Stuff I Assume You Know

• Format-Wide and what it’s good for• The basic Format-Table and Format-List

cmdlets• The –autosize parameter on Format-Table– And the implications for larger result sets

• Out-GridView• Other Output mechanisms (XML, CSV, etc.3)

Page 9: Deep dive formatting

REVIEWING THE BASICSDemo 1

Page 10: Deep dive formatting

Formatting Strings

• You can use either– -f operator, and format values into a composite

formatting string“This is {0}, formatting with {1}” –f “fun” , ”PowerShell”

– Use .ToString() to format a single value

Page 11: Deep dive formatting

Composite Formatting String

• Format of anchor– {<number, width:formatspecifier>}– Eg {0, 10: n4}

• Formatting<string with anchors> -f <array of values>• Produces a formatted string of array values• $date=get-date• “Today, {0}, is {1}” –f $date.date, $date.dayofweek

Page 12: Deep dive formatting

.ToString()

All base types have a .ToString() method Inherited from [Object] Relied on to convert the object to a printable form Can take parameters!

Base types have power over how to format Pass format string in call to .ToString()

$i = 23.123456; $i.tostring("N2") 23.12

Page 13: Deep dive formatting

.NET Format String Reference

Numeric format strings http://msdn.microsoft.com/en-us/library/427bttx3(VS.71).

aspx

Date and time format strings http://msdn.microsoft.com/en-us/library/97x6twsz(VS.71).

aspx

Page 14: Deep dive formatting

FORMATTING STRINGS/.TOSTRING()Demo 2

Page 15: Deep dive formatting

Formatting with Hash Tables

Uses a Hash Table with pre-defined key names: Name (or Label) - <string> Expression - <string> or <script block> FormatString - <string> Width - <int32> Alignment (value can be "Left", "Center", or "Right")

Send FT/FL hash table vs a property name

Page 16: Deep dive formatting

Hash Table Example$Pn=@{label="Process Name";

Expression={$_.Name}; Alignment="right"}

$Cpu=@{label="CPU Cost"; Expression={($_.CPU) * 21;.23};

FormatString=“C3"}

Get-Process notepad| Format-Table $Pn,$Cpu -auto

Page 17: Deep dive formatting

FORMAT-TABLE WITH HASH TABLESDemo 3

Page 18: Deep dive formatting

Formatting and .PS1XML

Two types of ps1xml Define/update types - *.types.ps1xml Define/update formatting - *.format.ps1xml

Default Format XML shipped with PowerShell Apps or OS additions add to this default set Stored in $Pshome

Page 19: Deep dive formatting

Format driven by object's type

• Discover a variable’s type name(s)– $variable.PSTYPENAMES

• To add a type name– $variable.PSTYPENAMES.ADD(“new name”)

• To clear all type names– $variable.PSTYPENAMES.CLEAR()

Page 20: Deep dive formatting

Formatting XML

To change how PowerShell formats a type? Create XML file Define views: table, list , etc Run Update-FormatData to add new view

Add this to $profile to persist the changes

Page 21: Deep dive formatting

XML – Overall Structure <Name>

Identifies the name of the view. <ViewSelectedBy>

Specifies the object type or types to which the view applies.

<GroupBy>

Specifies how items in the view should be combined in groups.

<TableControl> <ListControl> <WideControl> <ComplexControl>

Contain the tags that specify how each item is displayed.

Page 22: Deep dive formatting

Consider A New TypeAdd-Type @'public class aeroplane{ public string Model = "Boeing 737"; public int InFleet = 12; public int Range = 2400; public int Pax = 135;}'@

But what about the display?

Page 23: Deep dive formatting

XML File Structure<?xml version="1.0" encoding="utf-8" ?><Configuration> <ViewDefinitions> <View> <Name> … </Name> <ViewSelectedBy> <TypeName> … </TypeName> </ViewSelectedBy>

<TableControl> … </TableControl> <ListControl> … </ListControl>

</ViewDefinitions></Configuration>

Page 24: Deep dive formatting

TableControl Definition

<TableControl> <TableHeaders> <TableColumnHeader> <label>Passengers Carried</label><width>20</width> </TableColumnHeader> … </TableHeaders> <TableRowEntries> <TableRowEntry> <tableColumnItem><PropertyName>Pax</PropertyName></tablecolumnitem> </TableRowEntry> … </TableRowEntries></TableControl>

Page 25: Deep dive formatting

ListControl<ListControl> <ListEntries> <ListEntry> <ListItems> <ListItem> <PropertyName>Model</PropertyName> </ListItem> … <ListItems> <ListEntry> </ListEntries></Listcontrol>

Page 26: Deep dive formatting

Sticking It Together

Author, then save XML Prior to use, update format data

Update-FormatData –Prepend <format file name>

• Consider this pattern of development– PowerShell– PowerShell– Test stuff and find bugs– Exit– Edit the XML– PowerShell– Do it again

Page 27: Deep dive formatting

DISPLAY XMLDemo 4

Page 28: Deep dive formatting

But wait

Building XML is hard, boring, and just plain work

Instead of DIY

Easy out Great add on module

Page 29: Deep dive formatting

EASY OUTDemo 5

Page 30: Deep dive formatting

Summary

Formatting can be simple or complex Lots of alternatives – have it your way Keep it simple and extend to meet your needs Use EZ-out if you are accessing obscure WMI

classes or using uncommon .NET types• Demo 5


Top Related