PowerShell Module Manifest - automatically loading a Required Module

后端 未结 2 1695
一个人的身影
一个人的身影 2021-01-05 03:46

I am creating a module that depends on several other modules that I need loaded into the global environment. I tried creating a script and using ScriptsToProcess to import t

2条回答
  •  难免孤独
    2021-01-05 04:24

    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

提交回复
热议问题