Powershell import-module doesn't find modules

后端 未结 8 1029
暗喜
暗喜 2020-12-23 14:45

I\'m learning PowerShell and I\'m trying to build my own module library.

I\'ve written a simple module XMLHelpers.psm1 and put in my folder $home/

相关标签:
8条回答
  • 2020-12-23 15:17

    My finding with PS 5.0 on Windows 7: $ENV:PsModulePath has to end with a . This normally means it will load all modules in that path.

    I'm not able to add a single module to $env:PsModulePath and get it to load with Import-Module ExampleModule. I have to use the full path to the module. e.g. C:\MyModules\ExampleModule. I am sure it used to work.

    For example: Say I have the modules:

    C:\MyModules\ExampleModule
    C:\MyModules\FishingModule
    

    I need to add C:\MyModules\ to $env:PsModulePath, which will allow me to do

    Import-Module ExampleModule
    Import-Module FishingModule
    

    If for some reason, I didn't want FishingModule, I thought I could add C:\MyModules\ExampleModule only (no trailing \), but this doesn't seem to work now. To load it, I have to Import-Module C:\MyModules\ExampleModule

    Interestingly, in both cases, doing Get-Module -ListAvailable, shows the modules, but it won't import. Although, the module's cmdlets seem to work anyway.

    AFAIK, to get the automatic import to work, one has to add the name of the function to FunctionsToExport in the manifest (.psd1) file. Adding FunctionsToExport = '*', breaks the auto load. You can still have Export-ModuleMember -Function * in the module file (.psm1).

    These are my findings. Whether there's been a change or my computer is broken, remains to be seen. HTH

    0 讨论(0)
  • 2020-12-23 15:17

    try with below on powershell:

    Set-ExecutionPolicy -ExecutionPolicy Unrestricted
    import-module [\path\]XMLHelpers.psm1
    

    Instead of [] put the full path

    Full explanation of this and that

    0 讨论(0)
  • 2020-12-23 15:18

    1.This will search XMLHelpers/XMLHelpers.psm1 in current folder

    Import-Module (Resolve-Path('XMLHelpers'))
    

    2.This will search XMLHelpers.psm1 in current folder

    Import-Module (Resolve-Path('XMLHelpers.psm1'))
    
    0 讨论(0)
  • 2020-12-23 15:24

    Some plugins require one to run as an Administrator and will not load unless one has those credentials active in the shell.

    0 讨论(0)
  • 2020-12-23 15:29

    I had this problem, but only in Visual Studio Code, not in ISE. Turns out I was using an x86 session in VSCode. I displayed the PowerShell Session Menu and switched to the x64 session, and all the modules began working without full paths. I am using Version 1.17.2, architecture x64 of VSCode. My modules were stored in the C:\Windows\System32\WindowsPowerShell\v1.0\Modules directory.

    0 讨论(0)
  • 2020-12-23 15:31

    I think that the Import-Module is trying to find the module in the default directory C:\Windows\System32\WindowsPowerShell\v1.0\Modules.

    Try to put the full path, or copy it to C:\Windows\System32\WindowsPowerShell\v1.0\Modules

    0 讨论(0)
提交回复
热议问题