PowerShell模块未正确公开命令

时间:2018-06-12 17:36:07

标签: powershell powershell-module

我有一个奇怪的问题,因为我重新搭建了我的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

1 个答案:

答案 0 :(得分:0)

您的Get-Command -Module cfsdevops未显示get-podconfig,但gen-podconfig。因此,错误信息完全有意义。

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