functions/objects/Register-PSUObjectExpansion.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function Register-PSUObjectExpansion
{
<#
    .SYNOPSIS
        Registers a custom scriptblock for a type when processed by Expand-PSUObject.
     
    .DESCRIPTION
        Registers a custom scriptblock for a type when processed by Expand-PSUObject.
     
        Expand-PSUObject enables accelerated object expansion,
        by shortening the "Select-Object -ExpandProperty" call to "exp".
        It further has a list of default properties to expand,
        but it also allows implementing custom expansion rules, based on input type.
         
        This commands sets up these custom expansion rules.
        Define a scriptblock, it receives a single parameter - the input object to expand.
        The scriptblock is then responsible for expanding it and producing the desired output.
     
    .PARAMETER TypeName
        The name of the type to custom-expand.
     
    .PARAMETER ScriptBlock
        The scriptblock performing the expansion.
     
    .EXAMPLE
        PS C:\> Register-PSUObjectExpansion -TypeName 'MyModule.MyClass' -ScriptBlock $ScriptBlock
     
        Sets up a custom expansion rule for the 'MyModule.MyClass' class.
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, Position = 0)]
        [string]
        $TypeName,
        
        [Parameter(Mandatory = $true, Position = 1)]
        [scriptblock]
        $ScriptBlock
    )
    
    process
    {
        [PSUtil.Object.ObjectHost]::ExpandedTypes[$TypeName] = $ScriptBlock
    }
}