2

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

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
    }
}

该项目将成功构建并输出一个.dll名为MyProgram.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 的梯子。

4

1 回答 1

3

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

public class GetProgramOutputCmdlet : Cmdlet

于 2019-06-26T14:29:31.307 回答