我正在尝试通过创建 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.Automation
和Microsoft.PowerShell.5.ReferenceAssemblies
引用;通过 NuGet 的梯子。