1

I have a custom script module and one of the functions depends on the AWSPowerShell module. I attempted to put Import-Module AWSPowerShell inside that function, but when this runs on the server it fails and says no valid module is found. If I try Import-Module AWSPowerShell from the command line on the same server running as the same user it works fine. There seems to be something wrong with calling Import-Module from inside another module.

I saw that NestedModules can be used to specify dependencies so I added NestedModules @('AWSPowerShell') to the module manifest and removed the Import-Module AWSPowerShell from the function that needs it.

Now the error about AWSPowerShell happens at the point where I import my own custom module from the calling script.

The exact error is: Import-Module : The module to process 'AWSPowerShell', listed in field 'NestedModules' of module manifest 'C:\Program Files\WindowsPowerShell\Modules\...MyModule.psd1' was not processed because no valid module was found in any module directory.

I did notice that the AWSPowerShell module is installed in a different folder path than my custom module. My module is in C:\Program Files\WindowsPowerShell\Modules and AWSPowerShell is in C:\Program Files (x86)\AWS Tools\PowerShell.

How can I set this up so that AWSPowerShell can be loaded for use inside my module?

Update I made a copy of the AWSPowerShell module folder under C:\Program Files\WindowsPowerShell\Modules and now the module imports successfully using the NestedModules method. Everything I saw about NestedModules appeared to be for combining modules that were being developed for a bigger solution. I'm not sure about using this technique for importing a 3rd party module like this. I may be misusing this feature.

Matthew MacFarland
  • 2,413
  • 3
  • 26
  • 34
  • `C:\Program Files (x86)\AWS Tools\PowerShell` suggests that the module was installed from the _32-bit_ version of Windows PowerShell. Installing it from the 64-bit version should make it available to your module (assuming it is run from the 64-bit version) too; apart from that, the `RequiredModules` module-manifest entry is for referencing independently installed modules; please see my answer. – mklement0 Jun 23 '21 at 21:41
  • P.S.: The list of directories for PowerShell's [module auto-loading](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Modules#module-auto-loading), i.e. those directories in which `Import-Module` can discover modules by their name alone, is stored in `$env:PSModulePath`. – mklement0 Jun 23 '21 at 21:45

1 Answers1

5
  • The NestedModule module-manifest (.psd1) entry is for modules that are bundled with your module and available in your module's code only.

  • RequiredModules is for importing independently pre-installed modules, which are imported into the global scope when your module is imported, which means that their commands are also available to the caller.

Note: This answer shows how to specify RequiredModules modules to be installed on demand by declaring them as external dependencies, but the limitation appears to be that both the module and its dependency must come from the same repository and must be installed with Install-Module.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    This answer clears up many doubts. The 32/64 conflict is also a great lead on this problem. With this information I'm sure I can get this sorted out. – Matthew MacFarland Jun 23 '21 at 22:15