functions/utility/Register-PSFParameterClassMapping.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function Register-PSFParameterClassMapping
{
<#
 .SYNOPSIS
  Registers types to a parameter classes input interpretation.
  
 .DESCRIPTION
  The parameter classes shipped in PSFramework can be extended to support input of an unknown object type.
  In order to accomplish that, it is necessary to register the name of that type (and the properties to use) using this command.
  
  On input interpretation, it will check the TypeNames property on the PSObject for evaluation.
  This means you can also specify custom PSObjects by giving them a dedicated name.
  
 .PARAMETER ParameterClass
  The parameter class to extend.
  
 .PARAMETER TypeName
  The name of the type to register.
  
 .PARAMETER Properties
  The properties to check.
  When processing an object of this type, it will try to access the properties in this order, trying to make something fit the intended result.
  The first property that is a fit for the parameter class is chosen, other ones are ignored.
  
 .PARAMETER EnableException
  This parameters disables user-friendly warnings and enables the throwing of exceptions.
  This is less user friendly, but allows catching exceptions in calling scripts.
  
 .EXAMPLE
  PS C:\> Register-PSFParameterClassMapping -ParameterClass 'Computer' -TypeName 'microsoft.activedirectory.management.adcomputer' -Properties 'DNSHostName', 'Name'
  
  This extends the computer parameter class by ...
  - having it accept the type 'microsoft.activedirectory.management.adcomputer'
  - having it use the 'DNSHostName' property if available, falling back to 'Name' if necessary
#>

    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [PSFramework.Parameter.ParameterClasses]
        $ParameterClass,
        
        [Parameter(Mandatory = $true)]
        [string]
        $TypeName,
        
        [Parameter(Mandatory = $true)]
        [string[]]
        $Properties,
        
        [switch]
        $EnableException
    )
    
    begin
    {
        
    }
    process
    {
        switch ($ParameterClass)
        {
            "Computer"
            {
                [PSFramework.Parameter.ComputerParameter]::SetTypePropertyMapping($TypeName, $Properties)
            }
            default
            {
                Stop-PSFFunction -Message "Support for the $ParameterClass parameter class has not yet been added!" -EnableException $EnableException -Tag 'fail', 'argument' -Category NotImplemented
                return
            }
        }
    }
    end
    {
    
    }
}