为什么我不能从PSModulePath中的目录加载我的模块?

Chr*_*tle 2 powershell powershell-2.0 powershell-3.0

我正在尝试安装PS模块,以便可以从任何地方导入.我已经将我的安装目录添加到环境变量中并且不起作用,所以我尝试将我的模块放在PSCX目录中,因为我知道它可以工作.以下是演示此问题的命令行历史记录.为什么"import-module foo"不在这里工作?

C:\Program Files (x86)\PowerShell Community Extensions\pscx3\Pscx> $env:PSModulePath
C:\Users\chris\Documents\WindowsPowerShell\Modules;C:\Program Files (x86)\PowerShell     Community Extensions\Pscx3\;C:\windows\system32\WindowsPowerShell\v1.0\Modules\

# There's my module; foo.psm1
C:\Program Files (x86)\PowerShell Community Extensions\pscx3\Pscx> dir *.psm1


    Directory: C:\Program Files (x86)\PowerShell Community Extensions\pscx3\Pscx


Mode           LastWriteTime       Length Name
----           -------------       ------ ----
-a---     3/28/2013 10:37 AM           44 foo.psm1
-a---    10/20/2012  8:52 PM        19658 Pscx.psm1


# this works, as expected, so my PSModulePath seems correct.
C:\Program Files (x86)\PowerShell Community Extensions\pscx3\Pscx> import-module -force pscx

# I expect this to work since the module is in a PSModulePath directory... but it doesn't
C:\Program Files (x86)\PowerShell Community Extensions\pscx3\Pscx> import-module foo
import-module : The specified module 'foo' was not loaded because no valid module file was found in any module directory.
At line:1 char:1
+ import-module foo
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (foo:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

# Try again with the extension... nope.
C:\Program Files (x86)\PowerShell Community Extensions\pscx3\Pscx> import-module foo.psm1
import-module : The specified module 'foo.psm1' was not loaded because no valid module file was found in any module directory.
At line:1 char:1
+ import-module foo.psm1
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (foo.psm1:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

# I can import the module if I give the path
C:\Program Files (x86)\PowerShell Community Extensions\pscx3\Pscx> import-module ./foo.psm1 -verbose:$true
VERBOSE: Importing function 'fooobar'.
Run Code Online (Sandbox Code Playgroud)

CB.*_*CB. 7

您需要foo在此级别创建文件夹:

C:\Program Files (x86)\PowerShell Community Extensions\pscx3\foo
Run Code Online (Sandbox Code Playgroud)

并把你的foo.ps1文件放在那里.

然后你可以打电话

import-module foo
Run Code Online (Sandbox Code Playgroud)