How to list all the modules that this given module depends upon? (Particularly, I need their names and versions)
CodePudding user response:
To find the formally declared module dependencies - if any - of a given module via its module manifest (*.psd1):
Get-Module $someModule -ListAvailable | ForEach-Object {
$_.RequiredModules, $_.NestedModules.RequiredModules
}
Get-ModulereturnsSystem.Management.Automation.PSModuleInfoinstance(s) describing the specified module(s).-ListAvailablemakes sure that modules that aren't presently imported but are discoverable via auto-loading, are included in the lookup.$_.RequiredModuleslists the direct module dependencies, and$_.NestedModules.RequiredModulesthose of any nested modules, which is presumably rare.Tip of the hat to Ash.
Note that whether the information returned includes version information depends on whether a version is explicitly specified in the target module's manifest's RequiredModules entry.
Caveat: The above will not discover de facto dependencies that aren't also formally declared, based on the module's runtime behavior. Discovering such dependencies would be both nontrivial and brittle.
That said, if interactive discovery is an option, use the following as the first command from a pristine session started with -NoProfile, which may at least provide clues as to what dependent modules are de facto being imported at import time:
Import-Module -Verbose -Force $someModule
However, there could still be additional dependent modules that the module's commands import on demand, when they're first called.
Set-PSDebug with its -Trace parameter offers general tracing of code execution.
