Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. function ConvertTo-TableFormat {
  2. <#
  3. .SYNOPSIS
  4. Rebuild an object based on the Format Data for the object.
  5. .DESCRIPTION
  6. Allows an object to be rebuilt based on the view data for the object. Uses Select-Object to create a new PSCustomObject.
  7. #>
  8.  
  9. [CmdletBinding()]
  10. param (
  11. [Parameter(ValueFromPipeline)]
  12. [Object]$InputObject
  13. )
  14.  
  15. begin {
  16. $isFirst = $true
  17. }
  18.  
  19. process {
  20. $format = if ($isFirst) {
  21. $formatData = Get-FormatData -TypeName $InputObject.PSTypeNames |
  22. Select-Object -First 1
  23. if ($formatData) {
  24. $viewDefinition = $formatData.FormatViewDefinition |
  25. Where-Object Control -match 'TableControl'
  26.  
  27. for ($i = 0; $i -lt $viewDefinition.Control.Headers.Count; $i++) {
  28. $name = $viewDefinition.Control.Headers[$i].Label
  29.  
  30. $displayEntry = $viewDefinition.Control.Rows.Columns[$i].DisplayEntry
  31. if (-not $name) {
  32. $name = $displayEntry.Value
  33. }
  34.  
  35. $expression = switch ($displayEntry.ValueType) {
  36. 'Property' { $displayEntry.Value }
  37. 'ScriptBlock' { [ScriptBlock]::Create($displayEntry.Value) }
  38. }
  39.  
  40. @{ Name = $name; Expression = $expression }
  41. }
  42. }
  43. }
  44.  
  45. if ($format) {
  46. $InputObject | Select-Object -Property $format
  47. } else {
  48. $InputObject
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement