|
| 1 | +function Out-Alternate |
| 2 | +{ |
| 3 | + <# |
| 4 | + .SYNOPSIS |
| 5 | + Outputs alternate views |
| 6 | + .DESCRIPTION |
| 7 | + Outputs alternate views of an object or typename. |
| 8 | + |
| 9 | + While it is possible to display multiple views of an object in PowerShell, it's not as straightforward to see what those views are. |
| 10 | + |
| 11 | + Out-Alternate solves this problem, and is embeddable within a format file. |
| 12 | + .EXAMPLE |
| 13 | + Out-Alternate -TypeName "System.Diagnostics.Process" |
| 14 | + #> |
| 15 | + [Management.Automation.Cmdlet("Format", "Object")] |
| 16 | + param( |
| 17 | + # An input object. If this is provided, it will infer the typenames |
| 18 | + [Parameter(ValueFromPipeline)] |
| 19 | + [PSObject] |
| 20 | + $InputObject, |
| 21 | + |
| 22 | + # The typename of the alternate. |
| 23 | + # If this is not provided, it can be inferred from the `-InputObject`. |
| 24 | + [Alias('TypeName')] |
| 25 | + [string[]] |
| 26 | + $PSTypeName, |
| 27 | + |
| 28 | + # The name of the current view. |
| 29 | + # If this is provided, it will not be displayed as an alternate. |
| 30 | + [string] |
| 31 | + $CurrentView, |
| 32 | + |
| 33 | + # A prefix to each view. |
| 34 | + [string] |
| 35 | + $Prefix = "", |
| 36 | + |
| 37 | + # A suffix to each view. |
| 38 | + [string] |
| 39 | + $Suffix, |
| 40 | + |
| 41 | + # If set, will not padd the space between the name of the format control and the -View parameter |
| 42 | + [switch] |
| 43 | + $NoPadding |
| 44 | + ) |
| 45 | + |
| 46 | + process { |
| 47 | + # If no typename was provided |
| 48 | + if (-not $PSTypeName) { |
| 49 | + # and the input object has one |
| 50 | + if ($InputObject.pstypenames) { |
| 51 | + # use the input's PSTypeNames |
| 52 | + $PSTypeName = $InputObject.pstypenames |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + # If we have no PSTypename, return. |
| 57 | + if (-not $PSTypeName) { return } |
| 58 | + |
| 59 | + $views = # Get the views and force them into an array |
| 60 | + @(foreach ($typeName in $PSTypeName) { |
| 61 | + foreach ($view in (Get-FormatData -TypeName $TypeName).FormatViewDefinition) { |
| 62 | + $view |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + # Now we walk over each view |
| 67 | + @(foreach ($view in $views) { |
| 68 | + # If we provided a -CurrentView, and this is it, skip. |
| 69 | + if ($CurrentView -and $view.Name -eq $CurrentView) { continue } |
| 70 | + # Determine the format type by getting the control and removing "Control" from the typename. |
| 71 | + $formatType = $view.Control.GetType().Name -replace 'Control$' |
| 72 | + |
| 73 | + # By default, we pad space (for aesthetic reasons). |
| 74 | + if (-not $NoPadding) { # If `-NoPadding` was passed, we won't pad space. |
| 75 | + # Otherwise, always pad to 6. |
| 76 | + # (the length of 'Custom', the longest of the potential format types) |
| 77 | + $formatType = $formatType.PadRight(6, ' ') |
| 78 | + } |
| 79 | + # Now output the name of the format command and it's view. |
| 80 | + "Format-$formatType -View '$($view.Name -replace "'", "''")'" |
| 81 | + }) -join [Environment]::NewLine |
| 82 | + } |
| 83 | +} |
0 commit comments