functions/utility/Set-PSFTypeAlias.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
function Set-PSFTypeAlias
{
<#
 .SYNOPSIS
  Registers or updates an alias for a .NET type.
  
 .DESCRIPTION
  Registers or updates an alias for a .NET type.
  Use this function during module import to create shortcuts for typenames users can be expected to interact with directly.
  
 .PARAMETER AliasName
  The short and useful alias for the type.
  
 .PARAMETER TypeName
  The full name of the type.
  Example: 'System.IO.FileInfo'
  
 .PARAMETER Mapping
  A hashtable of alias to typename mappings.
  Useful to registering a full set of type aliases.
  
 .EXAMPLE
  PS C:\> Set-PSFTypeAlias -AliasName 'file' -TypeName 'System.IO.File'
  
  Creates an alias for the type 'System.IO.File' named 'file'
  
 .EXAMPLE
  PS C:\> Set-PSFTypeAlias -Mapping @{
   file = 'System.IO.File'
   path = 'System.IO.Path'
  }
  
  Creates an alias for the type 'System.IO.File' named 'file'
  Creates an alias for the type 'System.IO.Path' named 'path'
#>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    [CmdletBinding(DefaultParameterSetName = 'Name')]
    Param (
        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'Name')]
        [string]
        $AliasName,
        
        [Parameter(Mandatory = $true, Position = 1, ParameterSetName = 'Name')]
        [string]
        $TypeName,
        
        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'Hashtable')]
        [hashtable]
        $Mapping
    )
    
    begin
    {
        # Obtain a reference to the TypeAccelerators type
        $TypeAcceleratorType = [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")
    }
    process
    {
        foreach ($key in $Mapping.Keys)
        {
            $TypeAcceleratorType::Add($key, $Mapping[$key])
        }
        if ($AliasName)
        {
            $TypeAcceleratorType::Add($AliasName, $TypeName)
        }
    }
    end
    {
    
    }
}