2

我正在尝试安装一个 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'.
4

1 回答 1

7

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

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

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

然后你可以打电话

import-module foo
于 2013-03-28T19:26:48.313 回答