locked
PowerShell binary auto-load failure? RRS feed

  • Question

  • For various reasons I wrote a PowerShell module in C#. It runs correctly.

    I can run import-module against it and it loads and the function it exports operates as expected. 

    Now I want to have it auto-load so that when I open a PowerShell console it is ready for use.

    I create a folder for mymodule.dll called \mymodule. I put the .dll into that folder and move that structure to \Users\Me\Documents\WindowsPowerShell\Modules. That works without adding a .PSD1 or .PSM1 file. Briliant. But that only works when I am logged on. I use mymodule for some automation work I am doing. 

    Move that structure to \Program Files\WindowsPowerShell\Modules and it doesn't load properly anymore. Run Get-Module, the binary shows up correctly as well as the function. Run the function and you get the standard error about "MyModuleFunction is not recognized as the name of a cmdlet, function.... "yada yada yada.  Add-remove a .PSD1/.PSM1 and it makes no difference. Code-sign the PSM1 file, no difference, change execution policy (temporarily) to unrestricted, no change. 

    What in the world am I doing wrong?

    Sunday, July 16, 2017 10:09 PM

Answers

  • This is now solved - the issue was that I was using the binary module to replace an existing module and had not removed the reference to that module from the psm1 file. WOW that was a spectacular fail. I discovered this by loading my new module on a clean system and it loaded correctly as expected. I then went back and renamed the folder for the old module to prevent it from loading and the new one worked there too.
    • Marked as answer by John McCrae Wednesday, September 6, 2017 2:09 PM
    Wednesday, September 6, 2017 2:09 PM

All replies

  • You cannot see modules in a users folder.  Only the global modules paths are available to all.

    Do this:

    $env:psmodulepath  -split ';'

    These are all of the places where modules are found on my system.

    D:\Users\username\Documents\WindowsPowerShell\Modules
    C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
    D:\Users\username\Documents\WindowsPowerShell\Modules
    C:\Program Files\WindowsPowerShell\Modules
    C:\Program Files\SharePoint Online Management Shell\
    C:\Program Files\Common Files\Skype for Business Online\Modules\
    C:\Program Files (x86)\Microsoft SQL Server\140\Tools\PowerShell\Modules\
    C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\
    C:\Program Files\Microsoft SQL Server\110\Tools\PowerShell\Modules\

    You must have the module in the users path or the global path.


    \_(ツ)_/


    • Edited by jrv Sunday, July 16, 2017 10:20 PM
    Sunday, July 16, 2017 10:19 PM
  • Totally agree. This path shows up for you and I:

    C:\Program Files\WindowsPowerShell\Modules

    When I put my module there, I cannot use it as described above.

    Sunday, July 16, 2017 10:40 PM
  • The folder and DLL must have the same name and be the module name.  A PSD1 file should be added tot he folder.


    \_(ツ)_/

    Sunday, July 16, 2017 10:48 PM
  • Yup, already tested that and it doesn't change the behavior at all. Put the folder under \Program Files.... \Modules\Mymodule, it doesn't load properly.

    Put it under \User\John...\Modules\Mymodule, it works as expected.

    Sunday, July 16, 2017 10:59 PM
  • The account you are running under does not have the paths set correctly.  It is also passible you have a permissions issue.


    \_(ツ)_/

    Sunday, July 16, 2017 11:03 PM
  • Hi,
    Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.

    Best Regards,

    Frank

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, August 11, 2017 4:49 AM
  • Frank, 

    Thanks for checking in. I was never able to resolve this. I cannot find sufficient documentation to tell me whether my module is incorrectly built/compiled or whether I need to secure it/the module directory somehow differently. Please mark this closed - unresolved for now.

    Cheers

    John

    Saturday, August 19, 2017 3:22 PM
  • This is now solved - the issue was that I was using the binary module to replace an existing module and had not removed the reference to that module from the psm1 file. WOW that was a spectacular fail. I discovered this by loading my new module on a clean system and it loaded correctly as expected. I then went back and renamed the folder for the old module to prevent it from loading and the new one worked there too.
    • Marked as answer by John McCrae Wednesday, September 6, 2017 2:09 PM
    Wednesday, September 6, 2017 2:09 PM