Presentation is loading. Please wait.

Presentation is loading. Please wait.

Thomas Lee MCT PowerShell MVP.

Similar presentations


Presentation on theme: "Thomas Lee MCT PowerShell MVP."— Presentation transcript:

1 Thomas Lee (tfl@psp.co.uk)tfl@psp.co.uk MCT PowerShell MVP

2  MCT  PowerShell MVP  Worked with, trained and written about PowerShell for years!  SME on 6434 and TR on 10325 official MOC classes

3  I have 25 copies of PowerShell Plus  http://www.idera.com/Products/PowerShell/PowerShell- Plus/ or http://www.idera.com/Products/PowerShell/PowerShell- Plus/  http://tinyurl.com/psplus http://tinyurl.com/psplus  To win: 1. You need to email me with a code word. 2. Email me at tfl@psp.co.uktfl@psp.co.uk 3. The code word will be at the end of the presentation

4  Know a bit (or more) about PowerShell  At least to the level of the earlier Summit session!  You know what a cmdlet is, what the pipeline does and what objects are  And you want to do more with formatting  If you want to type-along with me, feel free!

5  Formatting by default  Formatting using Format-table, Format-List  Using.NET format Strings .ToString()  Formatting using Hash Tables  Formatting XML  Other output mechanisms  Some thoughts on style etc

6  PowerShell formats output by default  Useful for simple queries  Formatting is controlled by Format.PS1XML files  Seven are installed by default  LS $PSHOME\*.Format.PS1.XML  You can  You can edit built in Format.PS1XML files - but don’t…  Instead, add new ones and ‘install’ in Profile

7  The default formatting process Cmdlet or Pipeline Out-Default Format-Table or Format-List Formatted Output On Console

8  User Defined Formatting Cmdlet or Pipeline Format-Table or Format-List Out-Default Formatted Output On Console

9  Other Options Cmdlet or Pipeline Out-Gridview Text, CSV, XML Cmdlets

10  Cmdlets take objects from pipeline – create table/list  Default contents and ‘shape’ determined by.ps1xml  You can override defaults, for example:  GWMI win32_ComputerSystem | fl name,model  Can also take a hash table for precise format control

11  Used to print a single property as a table

12  The Format-* cmdlets format objects  Om the pipeline or via –Inputobject parameter  You can adjust the details of how you want your data to appear using the cmdlets, hash tables, format strings, etc  They produce format objects  The Out-* cmdlets take format objects and output it to the console (and elsewhere)

13  Format-Table  -Autosize  Waits till ALL objects are ready before formatting and auto- sizes the width of each column  - HidetableHeaders  Hides table headers  Format-List and Format-Table  -GroupBy  But you need to sort first

14  Objects in the pipeline are sent to Out-Default  Out-Default looks at the objects in the pipeline  If formatting instructions – send it to Out-Host  If objects - send to Format-List or Format-Table  Out-Default looks in.PS1XML for guidance  Select 1st view for guidance  Five or less properties – call Format-Table  Otherwise call Format-List  This creates format objects  Sent to Out-Default and then to the console

15  You can get basic output from PoweShell using default formatting  You can see more/different using Format-table/list directly  FL/FT typically used at the end of a pipeline  But PowerShell is built on.NET  Lets leverage.NET formatting

16  Composite String – with placeholders  "There are{0} ps1 files" –f (ls C:\foo\*.ps1).count  Placeholder Syntax  {index[,alignment]{:formatstring}]  Simple placeholder examples  {0}  {0,20}  {0,-20:n}  Use these along with the –f operator . NET formats values into the composite string!

17  Fixed decimal point  $a=123.4567898  "The Number is: {0:#.000}" –f $a  The Number is: 123.456  Currency  $a=123456.789123  "{0:c2}" –f $a  £123,456.79  Phone Number  $a=4255551212  "{0:(###) ###-####}" -f $a  (425) 555-1212

18  All types have a.ToString() method  Inherited from [Object]  Base types have power over how to format  Pass a format string to.ToString() to control formatting  Example  $i = 23.123456  $i.tostring("N2")  23.12

19  Format output for a single related set of values  Number of files  Number of files and their total size  Also use it to format one line in a table  Foreach ($file in $files) {display some aspect(s) of each file} .NET Formatting string used in  Composite format string  ToString()  Hash Tables (later in this talk)

20  Numeric format strings  http://msdn.microsoft.com/en-us/library/427bttx3(VS.71).aspx 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 http://msdn.microsoft.com/en-us/library/97x6twsz(VS.71).aspx  Enumeration format strings  http://msdn.microsoft.com/en-us/library/c3s1ez6e(VS.71).aspx http://msdn.microsoft.com/en-us/library/c3s1ez6e(VS.71).aspx

21 Simple Formatting

22  Using FT/FL, you can create a ‘calculated field’  Field defined by a Hash Table with pre-defined key names:  Name (or Label ) -  Expression - or  FormatString -  Width  Alignment - value can be "Left", "Center", or "Right")  Send FT/FL hash table vs a property name

23 $x1=@{label="Process Name"; Expression={$_.name}; alignment="right"} $x2=@{label="CPU Used"; Expression={$_.CPU}; FormatString="N3"} Get-Process notepad | Format-Table $x1,$x2 -auto

24 $x1=@{label="Process Name";Expression={$_.name}; alignment="right"} $x2=@{label="CPU Used"; Expression={$_.CPU};FormatString="N3"} Get-Process | Format-Table $x1,$x2 –autosize $x3=@{label="Process Name"; Expression={$_.name}} Get-Process | Format-List $x3,$x2

25  Give FT/FL better column/table headers  Sometimes property name is ugly/unhelpful  Display a calculated value  Covert a number into gb/mb etc  Expression = {$_.Length/1GB}  Coerce field into to nicer format easily

26  XML  Import-CliXML  Export-CliXML  ConvertTo-XML  CSV  Export-CSV (and Import-CSV)  Useful for interop with XML Applications

27  Uses WPF  You need latest.NET Framework  Creates sortable list in a separate window with criteria to help limit output

28  Out-GridView (and PowerShell ISE) need.NET 3.51  The rest of PowerShell requires.NET 2.0  Out-Gridview and ISE use WPF  By default, Out-Gridview displays same columns as FT  Clone the object first to see all object properties  Get-Process | Select-Object * | Out-Gridview

29 Hash tables and FT/FL

30  Seen default formatting  Used FT/FL and hash tables  Formatted using.NET  So let’s do create custom XML

31  Two types of ps1xml  Define/update types - *.types.ps1xml  Define/update formatting - *.format.ps1xml  Default Format XML shipped with PowerShell  Apps, OS additions, modules add to this default set  Stored in $PSHOME  DO NOT EDIT THESE FILES!  They are signed – editing breaks that!  Copy and edit them instead

32  Don’t like the way PowerShell formats a type?  Develop your own display XML  PowerShell ships with 7 format.ps1xml files  You can write your own  Define four views: table, list, wide, and complex  Do NOT edit existing files – make a copy and edit it  Run Update-FormatData to add new view  Add this to $profile to persist the change

33   Identifies the name of the view.   Specifies the object type or types to which the view applies.   Specifies how items in the view should be combined in groups.   Contain the tags that specify how each item is displayed.

34 Add-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?  Use FormatData.ps1xml

35 Airplane … Airplane … Saved as Aeroplane.formatdata.ps1xml Imported as needed/when needed

36 Passengers 12 … Pax …

37 Model …

38  Save XML to C  Update-FormatData c:\foo\aeroplane.format.ps1xml  Then: $Object = new-object aeroplane $object $object | fl

39  You can also extend existing types or add new ones  Create XML file describing your addition  Use Update-TypeData to incorporate it  Persist the change by updating $Profile

40 System.Object MSDN if (($global:MSDNViewer -eq $null) -or ($global:MSDNViewer.HWND -eq $null)) { $global:MSDNViewer = new-object -ComObject InterNetExplorer.Application} $Uri = "http://msdn2.microsoft.com/library/" + $this.GetType().FullName + ".ASPX" $global:MSDNViewer.Navigate2($Uri) $global:MSDNViewer.Visible = $TRUE

41  Create the XML  On your workstation  XCOPY during logon script?  Store it somewhere useful  C:\foo  Somewhere else??  Add the type/format information to your $Profile  Update-TypeData -Path  Update-FormatData –Path

42  Formatting can be simple or complex  Lots of alternatives – have it your way  Keep it simple and extend to meet your needs

43  I have free copies of PowerShell Pro  Idera’s PowerShell Development Tool  The first 25 folks to email me will get a link to download the software (and a license).  I gave you the email address at the start of this presentation  The codeword is “POWERSHELLISAWESOME!”  The first 25 to mail that code word to the address I gave earlier will win their prize!

44  Adding.MSDN() to types  http://blogs.msdn.com/powershell/archive/2006/06/24/644987 http://blogs.msdn.com/powershell/archive/2006/06/24/644987  Formatting - see TFL’s articles on formatting  http://tfl09.blogspot.com/2010/02/formatting-with- powershell.html http://tfl09.blogspot.com/2010/02/formatting-with- powershell.html .NET Formatting  http://msdn.microsoft.com/en-us/library/txafckwd(VS.71).aspx http://msdn.microsoft.com/en-us/library/txafckwd(VS.71).aspx  Thomas Lee’s Blogs/PowerShell site  http://tfl09.blogspot.com http://tfl09.blogspot.com  http://pshscripts.blogspot.com http://pshscripts.blogspot.com  http://www.reskit.net/psmc http://www.reskit.net/psmc

45


Download ppt "Thomas Lee MCT PowerShell MVP."

Similar presentations


Ads by Google