Get-Macro.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
function Get-Macro
{
    <#
    .Synopsis
        Gets Macros
    .Description
        Gets the currently loaded Macros
    #>

    param(
    # The name of the macro.
    # If not provided, all macros will be returned.
    [Parameter(ValueFromPipelineByPropertyName)]
    [string[]]
    $Name,

    # The macro prefix.
    # The default prefix is an underscore _.
    [Parameter(ValueFromPipelineByPropertyName)]
    [string]
    $Prefix = '_'
    )

    begin {
        $loadedModules = Get-Module
    }

    process {
        foreach ($lm in $loadedModules) {
            if ((-not $lm.Name) -or
                (-not $lm.Name.StartsWith($Prefix, 'OrdinalIgnoreCase'))) { continue }
            if ($Name -and
                $lm.Name -notcontains $Name) {

                $loadedModuleLikeName = $null

                foreach ($n in $name) {
                    if ($lm.Name -like $n -or $lm.Name -like "${prefix}$n") {
                        $loadedModuleLikeName = $lm
                        break
                    }
                }

                if (-not $loadedModuleLikeName) { continue}
            }
            if (-not $lm.PrivateData.IsPoshMacro) { continue }
            $lm
        }
    }
}