functions/utility/Get-PSFScriptblock.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
function Get-PSFScriptblock
{
<#
 .SYNOPSIS
  Access the scriptblocks stored with Set-PSFScriptblock.
  
 .DESCRIPTION
  Access the scriptblocks stored with Set-PSFScriptblock.
  
  Use this command to access scriptblocks designed for easy, processwide access.
  
 .PARAMETER Name
  The name of the scriptblock to request.
  It's mandatory for explicitly requesting a scriptblock, but optional to use with -List as a filter.
  
 .PARAMETER List
  Instead of requesting a specific scriptblock, list the available ones.
  This can be further filtered by using a wildcard supporting string as -Name.
  
 .EXAMPLE
  PS C:\> Get-PSFScriptblock -Name 'MyModule.TestServer'
  
  Returns the scriptblock stored as 'MyModule.TestServer'
  
 .EXAMPLE
  PS C:\> Get-PSFScriptblock -List
  
  Returns a list of all scriptblocks
  
 .EXAMPLE
  PS C:\> Get-PSFScriptblock -List -Name 'MyModule.TestServer'
  
  Returns scriptblock and meta information for the MyModule.TestServer scriptblock.
#>

    [OutputType([PSFramework.Utility.ScriptBlockItem], ParameterSetName = 'List')]
    [OutputType([System.Management.Automation.ScriptBlock], ParameterSetName = 'Name')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidDefaultValueForMandatoryParameter", "")]
    [CmdletBinding(DefaultParameterSetName = 'Name')]
    param (
        [Parameter(ParameterSetName = 'List')]
        [Parameter(Mandatory = $true, ParameterSetName = 'Name', ValueFromPipeline = $true)]
        [string[]]
        $Name = '*',
        
        [Parameter(Mandatory = $true, ParameterSetName = 'List')]
        [switch]
        $List
    )
    
    begin
    {
        [System.Collections.ArrayList]$sent = @()
        $allItems = [PSFramework.Utility.UtilityHost]::ScriptBlocks.Values
    }
    process
    {
        :main foreach ($nameText in $Name)
        {
            switch ($PSCmdlet.ParameterSetName)
            {
                'Name'
                {
                    if ($sent -contains $nameText) { continue main }
                    $null = $sent.Add($nameText)
                    [PSFramework.Utility.UtilityHost]::ScriptBlocks[$nameText].ScriptBlock
                }
                'List'
                {
                    foreach ($item in $allItems)
                    {
                        if ($item.Name -notlike $nameText) { continue }
                        if ($sent -contains $item.Name) { continue }
                        $null = $sent.Add($item.Name)
                        $item
                    }
                }
            }
        }
    }
}