使用 Import-Module 导入时,C# 二进制文件中的 Cmdlet 不导出

Cmdlet in C# binary not exporting when imported with Import-Module

我正在尝试通过创建 PowerShell Cmdlet 为我的一个 C# 程序创建接口。我最初将项目(在 visual studio 中)创建为 Console Application。我已经在项目属性中将输出类型切换为 Class Library 并注释掉了主要的 class。然后我在同一个命名空间中添加了一个Cmdlet class。

using System.Management.Automation;
// various other using dependencies

namespace MyProgram
{
    // This class used to contain the main function
    class ProgramLib
    {
        // various static methods
    }

    [Cmdlet(VerbsCommon.Get, "ProgramOutput")]
    [OutputType(typeof(DataTable))]
    class GetProgramOutputCmdlet : Cmdlet
    {
        protected override void ProcessRecord()
        {
            // Code using ProgramLib methods
        }

        // Begin/End Processing omitted for brevity
    }
}

项目将构建成功并输出一个名为MyProgram.dll.

.dll文件

然后我可以通过 PowerShell 导航到项目目录并导入程序集而不会出现错误:

PS> Import-Module .\MyProgram.dll -Verbose -Force
VERBOSE: Loading module from path 'C:\my\current\path\MyProgram.dll'.
PS> 

但是,它似乎没有加载我的 Cmdlet:

PS> Get-ProgramOutput
Get-ProgramOutput : The term 'Get-ProgramOutput' is not recognized as the name of a
cmdlet, function, script file, or operable program.

为什么我的 Cmdlet 没有导出?

我在我的项目中包含了 System.Management.AutomationMicrosoft.PowerShell.5.ReferenceAssemblies 参考;通过 NuGet 的阶梯。

C# 中 class 的默认访问修饰符是 internal。如果您将 "public" 添加到您的 cmdlet class 定义中,您应该在导入模块时看到您的 cmdlet。

public class GetProgramOutputCmdlet : Cmdlet