【问题标题】:PowerShell module not exposing commands properlyPowerShell 模块未正确公开命令
【发布时间】:2018-06-12 17:36:06
【问题描述】:

自从我重新搭建了 PowerShell 模块后,我遇到了一个奇怪的问题。

cfsdevops.psm1 相当简单,只需自动导入/private/public 中的所有.ps1 文件。

#Get public and private function definition files.
$Public  = @( Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue )
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue )

#Dot source the files
Foreach($import in @($Public + $Private))
{
    Try
    {
        . $import.fullname
    }
    Catch
    {
        Write-Error -Message "Failed to import function $($import.fullname): $_"
    }
}

Export-ModuleMember -Function $Public.Basename

一旦我将所有文件发布到我的模块路径目录并尝试运行命令,我会收到一条错误消息,表明它不是有效的命令:

PS C:\> get-podconfig
get-podconfig : The term 'get-podconfig' 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
+ get-podconfig
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (get-podconfig:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

但是,当我随后运行“Get-Command -Module cfsdevops”时,它会列出所有可用的命令,并且该会话中的工作正常。

PS C:\> Get-Command -Module cfsdevops

CommandType     Name                    Version    Source
-----------     ----                    -------    ------
Function        add-deploymanifest      4.1.0      cfsdevops
Function        add-podconfig           4.1.0      cfsdevops
Function        add-tenant              4.1.0      cfsdevops
Function        gen-podconfig           4.1.0      cfsdevops
Function        get-aobcredential       4.1.0      cfsdevops
Function        get-podconfig           4.1.0      cfsdevops
Function        get-servertraffic       4.1.0      cfsdevops
Function        new-deploymanifest      4.1.0      cfsdevops
Function        publish-octopus         4.1.0      cfsdevops
Function        publish-webconfig       4.1.0      cfsdevops
Function        set-aobcredential       4.1.0      cfsdevops
Function        set-octopusapikey       4.1.0      cfsdevops

【问题讨论】:

  • “gen-podconfig”是错字吗?

标签: powershell powershell-module


【解决方案1】:

您的Get-Command -Module cfsdevops 不显示get-podconfig,而是gen-podconfig。因此,错误消息完全有意义。

检查脚本文件名和您的 PowerShell 模块清单 (psd1-File) 是否有拼写错误。

【讨论】:

  • 太好奇了,我上面的例子我粗心大意——实际功能没有回来。然而,从那以后它奇迹般地起作用了。未进行任何更改。
猜你喜欢
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 2018-10-07
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多