deep dive formatting

Download Deep dive formatting

Post on 24-May-2015

967 views

Category:

Technology

4 download

Embed Size (px)

TRANSCRIPT

  • 1. How Do They Do That?Formatting with PowerShellThomas Lee

2. Agenda Review the basics Composite Formatting and .ToString Formatting with Format-tables Using Format XML EasyOutput (thanks James!) DEMOS!! 3. 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 PowerShellScripts Blog 4. 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 5. Formatting Rules If format.ps1xml defines format for a type Use that If not, does type.ps1xml define a defaultproperty 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 6. Format-Table and Format-List Format-List/Format-Table take objects frompipeline 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 formatcontrol 7. Occurrence Type Drives Formatting An objects type determine how PowerShellrenders instances .PS1XML defines the format for many types Others use the last resort rules (1-4/5+) 8. Other Stuff I Assume You Know Format-Wide and what its good for The basic Format-Table and Format-Listcmdlets The autosize parameter on Format-Table And the implications for larger result sets Out-GridView Other Output mechanisms (XML, CSV, etc.3) 9. Demo 1REVIEWING THE BASICS 10. Formatting Strings You can use either -f operator, and format values into a compositeformatting string This is {0}, formatting with {1} f fun , PowerShell Use .ToString() to format a single value 11. Composite Formatting String Format of anchor {} Eg {0, 10: n4} Formatting -f Produces a formatted string of array values $date=get-date Today, {0}, is {1} f $date.date, $date.dayofweek 12. .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 13. .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 14. Demo 2FORMATTING STRINGS/.TOSTRING() 15. Formatting with Hash Tables Uses a Hash Table with pre-defined keynames: Name (or Label) - Expression - or