PoshCode Logo PowerShell Code Repository

Add-FormatTableIndexPara by Lee Holmes 4 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2129"></script>download | new post

From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes

  1. ##############################################################################
  2. ##
  3. ## Add-FormatTableIndexParameter
  4. ##
  5. ## From Windows PowerShell Cookbook (O'Reilly)
  6. ## by Lee Holmes (http://www.leeholmes.com/guide)
  7. ##
  8. ##############################################################################
  9.  
  10. <#
  11.  
  12. .SYNOPSIS
  13.  
  14. Adds a new -IncludeIndex switch parameter to the Format-Table command
  15. to help with array indexing.
  16.  
  17. .NOTES
  18.  
  19. This commands builds on New-CommandWrapper, also included in the Windows
  20. PowerShell Cookbook.
  21.  
  22. .EXAMPLE
  23.  
  24. PS >$items = dir
  25. PS >$items | Format-Table -IncludeIndex
  26. PS >$items[4]
  27.  
  28. #>
  29.  
  30. Set-StrictMode -Version Latest
  31.  
  32. New-CommandWrapper Format-Table `
  33.     -AddParameter @{
  34.         @{
  35.             Name = 'IncludeIndex';
  36.             Attributes = "[Switch]"
  37.         } = {
  38.  
  39.         function Add-IndexParameter {
  40.             begin
  41.             {
  42.                 $psIndex = 0
  43.             }
  44.             process
  45.             {
  46.                 ## If this is the Format-Table header
  47.                 if($_.GetType().FullName -eq `
  48.                     "Microsoft.PowerShell.Commands.Internal." +
  49.                     "Format.FormatStartData")
  50.                 {
  51.                     ## Take the first column and create a copy of it
  52.                     $formatStartType =
  53.                         $_.shapeInfo.tableColumnInfoList[0].GetType()
  54.                     $clone =
  55.                         $formatStartType.GetConstructors()[0].Invoke($null)
  56.  
  57.                     ## Add a PSIndex property
  58.                     $clone.PropertyName = "PSIndex"
  59.                     $clone.Width = $clone.PropertyName.Length
  60.  
  61.                     ## And add its information to the header information
  62.                     $_.shapeInfo.tableColumnInfoList.Insert(0, $clone)
  63.                 }
  64.  
  65.                 ## If this is a Format-Table entry
  66.                 if($_.GetType().FullName -eq `
  67.                     "Microsoft.PowerShell.Commands.Internal." +
  68.                     "Format.FormatEntryData")
  69.                 {
  70.                     ## Take the first property and create a copy of it
  71.                     $firstField =
  72.                         $_.formatEntryInfo.formatPropertyFieldList[0]
  73.                     $formatFieldType = $firstField.GetType()
  74.                     $clone =
  75.                         $formatFieldType.GetConstructors()[0].Invoke($null)
  76.  
  77.                     ## Set the PSIndex property value
  78.                     $clone.PropertyValue = $psIndex
  79.                     $psIndex++
  80.  
  81.                     ## And add its information to the entry information
  82.                     $_.formatEntryInfo.formatPropertyFieldList.Insert(
  83.                         0, $clone)
  84.                 }
  85.  
  86.                 $_
  87.             }
  88.         }
  89.  
  90.         $newPipeline = { __ORIGINAL_COMMAND__ | Add-IndexParameter }
  91.     }
  92. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me