【问题标题】:Powershell module not loadingPowershell模块未加载
【发布时间】:2015-08-08 20:16:45
【问题描述】:

我正在尝试加载执行自定义 cmdlet 函数的 PowerShell 模块,但我无法加载它...我已经应用了severalpreviousquestions 的解决方案,但现在我'我只是在转圈。以下是我到目前为止所做的规范以及返回的特定错误。请注意,由于我是 PowerShell 和一般编程的新手,所以我的问题不是文件路径问题,而是我的实际函数中的逻辑错误,这不会让我感到惊讶:

  1. 我为自定义 cmdlet 创建了一个配置文件函数,它允许我 从两个不同的路径打开项目文件:

           function push-project( $project )
        {
            pushd ~/Documents/Visual Studio 2015/Projects/$project;
            pushd ~/Documents/GitHub/$project;
        }
    
    New-Alias -Name pp -Value push-project;
    
  2. 我通过将函数保存为ProfileFunctions.psm1 创建了一个模块 在以下目录中:

    ~\Documents\WindowsPowerShell\Modules\ProfileFunctions\ProfileFunctions.psm1
  3. 要调用该函数,根据其语法,我在 PS 控制台窗口中输入 pp $projectName,但返回的错误是标准无法识别的:

    pp : The term 'pp' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
    spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:1
    
    
    • pp MyFirstApp
    • ~~
    • CategoryInfo : ObjectNotFound: (pp:String) [], CommandNotFoundException
    • FullyQualifiedErrorId : CommandNotFoundException

【问题讨论】:

  • 如果你Import-Module ProfileFunctions 会发生什么?
  • 如果你使用常规的函数名是否有效?
  • @Eris,我在尝试导入时收到“不存在有效的模块文件”错误。所以我猜 PS 甚至一开始都没有创建一个。
  • 如果你的模块存在,当你运行Get-Module -ListAvailable时它会被列出来。也就是说,如果您的目标只是将该功能加载到您的 powershell 环境中,我宁愿只使用 $profile (您知道我的意思吗)?还是你有其他理由使用模块?
  • @ElliotEwert 我在我的个人资料中使用自定义函数。所以在这里讨论一些话题,但如果它有帮助 - 创建一个配置文件(如果你没有)你可以运行New-Item -Type file $profile,然后例如ise $profile 编辑该文件。如果您在其中添加函数和别名,它应该可以工作。每次打开 Powershell 时,Powershell 都会自动加载 $profile。它可能会阻止您使用您可以使用Set-ExecutionPolicy RemoteSigned 设置的执行策略。抱歉,如果您已经知道这一切。

标签: powershell module


【解决方案1】:

我将您的代码复制并粘贴到我这里的 Windows 8.1 机器中。它工作得很好(除了文件夹名称不存在,因为我没有 Visual Studio)。

我想到的可能会阻止您的模块工作的事情:

该文件实际上称为ProfileFunctions.psm1.ps1ProfileFunctions.psm1.txt 之类的。

Modules 文件夹保存在其他人的文档文件夹或公共文档文件夹中。

您不小心在文件夹名称中添加了一个空格(必须是 WindowsPowerShell,而不是 Windows PowerShell)。

【讨论】:

  • 您的猜测是正确的,隐藏文件扩展名是“.psm1.ps1”的问题的一部分。事实证明,一旦我纠正了这一点,@Jower 的解决方案也适用。谢谢。
【解决方案2】:

我认为您的问题是 Alias pp 没有从模块中导出。

您可以在模块外部定义别名,如预期的那样,或者使用从模块中显式导出它。

Export-ModuleMember -Function pushproject -Alias pp

在这篇文章中查找更多详细信息Unable to Create a Powershell Alias in a script Module

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-29
    • 2017-09-22
    • 2021-11-28
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多