Parts/GetExtensionModule.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
<#
.Synopsis
    Gets Extension Modules
.Description
    Gets modules that can extend or plugin to other existing modules.
 
    A module is considered an extension module if:
 
    * It Requires this module name ($Module.RequiredModules -contains $ModuleName)
    * It Tags this module name ($Module.PrivateData.PSdata.Tags -contains $ModuleName)
    * It has private data for this module ($Module.PrivateData.$ModuleName exists)
    * It has attached data for this module ($module.$ModuleName exists)
#>

param(
    # The name of the extensible module.
    [Parameter(Mandatory,Position=0)]
    [string]
    $ModuleName
)

$loadedModules = Get-Module

foreach ($module in $loadedModules) {
    $requiredModuleNames = @(foreach ($_ in $module.RequiredModules) {$_.Name })
    if ($requiredModuleNames -notcontains $ModuleName -and
        $module.PrivateData.PSData.Tags -notcontains $ModuleName -and
        -not $module.PrivateData.$ModuleName -and
        -not $Module.$ModuleName) { continue }
    $module
}