开发者

PowerShell Module Manifest - automatically loading a Required Module

开发者 https://www.devze.com 2023-02-21 00:46 出处:网络
I am creating a module that depends on several other mo开发者_如何学Godules that I need loaded into the global environment. I tried creating a script and using ScriptsToProcess to import the modules,

I am creating a module that depends on several other mo开发者_如何学Godules that I need loaded into the global environment. I tried creating a script and using ScriptsToProcess to import the modules, but it looks like the check for RequiredModules happens before ScriptstoProcess runs.

Is there a nice, clean way in a Module Manifest to both Require a module and load it automatically if its not already loaded? If the module could not be loaded, then the RequiredModule would throw an error.


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.


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.

  • about_Requires
0

精彩评论

暂无评论...
验证码 换一张
取 消