PowerShell Module Manifest - automatically loading a Required Module

跟風遠走 提交于 2019-11-30 19:46:24
Jaykul

In PowerShell 3 and up, RequiredModules are automatically loaded

It's also the only way to make sure people using PowerShellGet (i.e. the PowerShell Gallery) install your dependencies, if you're going to distribute the module.

It would still fail if the required modules are missing, but otherwise works exactly the way you'd wish it to.

In PowerShell 2, there's no way to automatically load RequiredModules

In either case, users can manually load the requirements by typing Import-Module RequiredModule, YourModule -- they won't get a second instance if it's already imported ...

You can also specify the module in NestedModules instead. Even in PowerShell 2, those get loaded "inside" your module, but don't seem to negatively affect resources when they're already loaded. However, as @JasonMArcher reminded me, in PowerShell 2, NestedModules get unloaded along with your module if your module gets unloaded (via Remove-Module), and this happens even if they were pre-loaded separately by the user, which can end up producing really weird bug reports since your users won't expect that.

The other option, which works in all versions of PowerShell, is to call Import-Module at the top of your module (in a psm1 script, after checking to make sure the module's not already loaded) with the -ErrorAction Stop set so that the import of your module will fail if the import of the dependent module fails.

if (!(Get-Module Dependency)) { ## Or check for the cmdlets you need
    ## Load it nested, and we'll automatically remove it during clean up
    Import-Module Dependency -ErrorAction Stop
}

Actually, if you wanted to check for versions ...

if (!(Get-Module Dependency | Where { $_.Version -ge "2.5" )) { 
    ## Load version 2.5 (or newer), or die 
    Import-Module Dependency -Version 2.5 -ErrorAction Stop
}

Just remember that this doesn't serve as documentation, so if you distribute the module, your users will not know about the dependencies.

tfl

PowerShell V3 works somewhat differently. Required modules are now loaded when you load a manifest with that key specified. Also, removing the module does not unload the required module, no matter how that module was loaded. Interestingly, -Force does not seem to show the autoloading of the required module(s).


Edit: If your module is not in one of the default locations, you will need to add the path to that module (or the parent folder under which the module resides) to the $env:PSModulePath variable.


Side note: since PSv3 there is support for the #Requires command in scripts. You can use this to (1) attempt to load a module dependency in your script and (2) terminate if the module is not/cannot be loaded.

#Requires -Modules ModuleName1,ModuleName2

This also needs the modules to be somewhere under $env:PSModulePath, which must be set before running the script.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!