PoshCode / Configuration

A module to help other modules have settings
MIT License
176 stars 27 forks source link

Cannot import-configuration when the module folder isn't the module name #21

Closed Jackbennett closed 2 years ago

Jackbennett commented 5 years ago

Even though you can happily test a module as "c:\my-powershell\example.psm1" with import-module -force c:\my-powershell\example.psm1 using import-configuration will create the error below. You'd have to rename the path "c:\example\example.psm1". I appreciate that's the name scheme you've got to have when putting the module into psmodulePath but for development it's a little restrictive.

Inside my .psm1 with $Script:Config = import-configuration results in the following error

Get-Module : The specified module 'N:\Documents\src\ps-schoolgroups' was not found. Update the Name parameter to
point to a valid path, and then try again.
At \\homeDirectory\jbennett\Documents\WindowsPowerShell\Modules\Configuration\1.3.0\Configuration.psm1:99 char:27
+ ...            if($mi2 = Get-Module $mi.ModuleBase -ListAvailable | Where ...
+                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (N:\Documents\src\ps-schoolgroups:String) [Get-Module], FileNotFou
   ndException
    + FullyQualifiedErrorId : Modules_ModuleNotFoundForGetModule,Microsoft.PowerShell.Commands.GetModuleCommand

error from line 99

I don't know if this is behaviour worth improving but I couldn't make a workaround if you might be able to suggest one. I have 72 projects in a folder it's quite nice organizing the powershell ones as prefixed with ps-<modulename>. Of course they don't get installed to psmodulepath with that pattern.

kilasuit commented 5 years ago

@Jackbennett I would suggest that you follow the practices to have your module in a folder of C:\my-powershell\example\example.psm1 because if you want to package up the module further down the line to share with others it is better to have it all contained in a folder under the name of the module as this is what Publish-Module will look for.

hope the above helps

Jaykul commented 5 years ago

TL:DR The workaround is to use the ManualOverride parameterset and pass the -Company (or -Author) and the module -Name ...

Basically, the problem you're having @Jackbennett is that the module stores configuration based on the author and module name, E.g.: AppData\PowerShell\CompanyOrAuthor\ModuleName

In order to determine the Company or Author automatically, it needs to have a manifest, and be installed somewhere I can find it (i.e. Get-Module YourModule -ListAvailable needs to work) -- then it will read the metadata from there.

I can definitely look at putting an -ErrorAction SilentlyContinue in that to prevent it from throwing, but the best case scenario would be that your data will end up in AppData\PowerShell\AnonymousModules\ModuleName ...

So, if you need to run from outside the PSModulePath, and especially if you want to avoid creating a manifest -- you have two options:

  1. Hard-code the author and module name parameters in the call
  2. Use Import/Export Metadata instead of Configuration and specify a path such as $PSScriptRoot\MyModuleConfiguration.psd1 so that you can read you configuration from the folder you're in without messing with the automatic config storage location guessing...
Jaykul commented 5 years ago

If anyone's looking for an easy hacktoberfest contribution, going through the commands and making sure that there's an example of the ManualOverride parameterset on all of them would be a good idea 😉

Jaykul commented 3 years ago

I was thinking just now that Get-Module -ListAvailable works with a path to a PSD1 (rather than to the ModuleBase), so it's possible this could be fixed (now) with a code change for PS5.1+