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

时间:2013-03-28 18:00:06

标签: powershell powershell-v2.0 powershell-v3.0

我试图安装PS模块,以便可以从任何地方导入。我已经将我的安装目录添加到环境变量中并且没有用,所以我尝试将我的模块放在PSCX目录中,因为我知道它可以工作。以下是演示此问题的命令行历史记录。为什么没有"导入模块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'.

1 个答案:

答案 0 :(得分:7)

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

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

并将foo.ps1文件放在那里。

然后你可以打电话

import-module foo