internal/functions/Import-DbaCmdlet.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
function Import-DbaCmdlet {
<#
    .SYNOPSIS
        Loads a cmdlet into the current context.
     
    .DESCRIPTION
        Loads a cmdlet into the current context.
        This can be used to register a cmdlet during module import, making it easy to have hybrid modules publishing both cmdlets and functions.
        Can also be used to register cmdlets written in PowerShell classes.
     
    .PARAMETER Name
        The name of the cmdlet to register.
     
    .PARAMETER Type
        The type of the class implementing the cmdlet.
     
    .PARAMETER HelpFile
        Path to the help XML containing the help for the cmdlet.
     
    .PARAMETER Module
        Module to inject the cmdlet into.
     
    .EXAMPLE
        PS C:\> Import-DbaCmdlet -Name Get-Something -Type ([GetSomethingCommand])
         
        Imports the Get-Something cmdlet into the current context.
     
    .EXAMPLE
        PS C:\> Import-DbaCmdlet -Name Get-Something -Type ([GetSomethingCommand]) -Module (Get-Module PSReadline)
         
        Imports the Get-Something cmdlet into the PSReadline module.
     
    .NOTES
        Original Author: Chris Dent
        Link: https://www.indented.co.uk/cmdlets-without-a-dll/
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [String]
        $Name,
        
        [Parameter(Mandatory)]
        [Type]
        $Type,
        
        [string]
        $HelpFile,
        
        [System.Management.Automation.PSModuleInfo]
        $Module
    )
    
    begin {
        $scriptBlock = {
            param (
                [String]
                $Name,
                
                [Type]
                $Type,
                
                [string]
                $HelpFile
            )
            
            $sessionStateCmdletEntry = New-Object System.Management.Automation.Runspaces.SessionStateCmdletEntry(
                $Name,
                $Type,
                $HelpFile
            )
            
            # System.Management.Automation.Runspaces.LocalPipeline will let us get at ExecutionContext.
            # Note: $ExecutionContext is *not* an instance of this object.
            $pipelineType = [PowerShell].Assembly.GetType('System.Management.Automation.Runspaces.LocalPipeline')
            $method = $pipelineType.GetMethod(
                'GetExecutionContextFromTLS',
                [System.Reflection.BindingFlags]'Static,NonPublic'
            )
            
            # Invoke the method to get an instance of ExecutionContext.
            $context = $method.Invoke(
                $null,
                [System.Reflection.BindingFlags]'Static,NonPublic',
                $null,
                $null,
                (Get-Culture)
            )
            
            # Get the SessionStateInternal type
            $internalType = [PowerShell].Assembly.GetType('System.Management.Automation.SessionStateInternal')
            
            # Get a valid constructor which accepts a param of type ExecutionContext
            $constructor = $internalType.GetConstructor(
                [System.Reflection.BindingFlags]'Instance,NonPublic',
                $null,
                $context.GetType(),
                $null
            )
            
            # Get the SessionStateInternal for this execution context
            $sessionStateInternal = $constructor.Invoke($context)
            
            # Get the method which allows Cmdlets to be added to the session
            $method = $internalType.GetMethod(
                'AddSessionStateEntry',
                [System.Reflection.BindingFlags]'Instance,NonPublic',
                $null,
                $sessionStateCmdletEntry.GetType(),
                $null
            )
            # Invoke the method.
            $method.Invoke($sessionStateInternal, $sessionStateCmdletEntry)
        }
    }
    
    process {
        if (-not $Module) { $scriptBlock.Invoke($Name, $Type, $HelpFile) }
        else { $Module.Invoke($scriptBlock, @($Name, $Type, $HelpFile)) }
    }
}