c# - 在 C# 中创建 PowerShell Cmdlet - 管道链

标签 c# powershell powershell-cmdlet

我有一些 C# 类,我想在管道中使用它们,我已经看过有关它的文章,但我还无法做到。

这是我现在的使用方式:

$suite = [MyProject.SuiteBuilder]::CreateSuite('my house')

$houseSet = $suite.AddSet('doors', 'These represents doors')
$houseSet.AddOption('blue', 'kitchen')
$houseSet.AddOption('black', 'bedreoom')
$houseSet.AddOption('white', 'toilet')

我希望能够像管道一样使用它:

$suite = [MyProject.SuiteBuilder]::CreateSuite('my house')

$suite | AddSet('doors', 'These represents doors') `
       | AddOption('blue', 'kitchen') `
       | AddOption('black', 'bedreoom') `
       | AddOption('white', 'toilet')

这是我的 C# 类:

//SuiteBuilder.cs
public static class SuiteBuilder
{
    public static Suite CreateTestSuite(string name)
    {
        return new Suite(name);
    }
}

//Suite.cs
public class Suite : PSCmdlet
{
    public string Name { get; set; }
    public IEnumerable<Set> Sets { get; set; }

    public Suite(string name)
    {
        Name = name;
        Sets = new List<Set>();
    }

    // call this method in pipeline
    public Set AddSet(string type, string description)
    {
        Sets.Add(new Set(type, description));
        return Sets.Last();
    }
}


//Set.cs
public class Set : PSCmdlet
{
    public string Type { get; set; }
    public string Description { get; set; }
    public IEnumerable<Option> Options { get; set; }

    public Set(string type, string description)
    {
        Type = type;
        Description = description;
        Options = new List<Option>();
    }

    // call this method from pipeline
    public Set AddOption(string color, string place)
    {
        Options.Add(new Option(color, place));
        return this;
    }
}


//option.cs
public class Option : PSCmdlet
{
    public string Color { get; set; }
    public string Place { get; set; }

    public Option(string color, string place)
    {
        Color = color;
        Place = place;
    }
}

我正在努力使这些函数可以在管道形式中调用。

我还在需要调用的每个注释之前添加了一条注释,例如在管道中调用此方法

最佳答案

简而言之,您需要:

  • 使用 [Parameter(ValueFromPipeline =true)] 接受来自管道的参数
  • 通过调用WriteObject向管道提供输出过程中的方法

详细的逐步解答

在这篇文章中,我将稍微重构一下您的代码,并向您展示如何创建 Powershell Cmdlet在 C# 中,以及如何定义参数接受来自管道的参数以及向管道提供输出。然后你可以轻松地编写如下内容:

$suite = [MyCmdLets.Suite]::New("suite1")
$suite | Add-Set "type1" "desc1"`
       | Add-Option "color1" "place1"`
       | Add-Option "color2" "place2" | Out-Null

为此,请按照下列步骤操作:

  1. 创建一个 C# 类库项目(例如将其命名为 MyCmdlets )
  2. 安装包 Microsoft.PowerShell.5.ReferenceAssemblies
  3. 创建独立于 PowerShell 的模型类。 (请参阅帖子底部的代码)
  4. 创建 cmdlet 时请考虑以下注意事项:(请参阅帖子底部的代码)

    • 为每个 cmdlet 创建一个 C# 类
    • 源自 Cmdlet
    • CmdletAttribute 装饰类指定动词和动词后面的名称的属性,例如,如果您想要 Add-Set ,使用[Cmdlet(VerbsCommon.Add, "Set")] .
    • 如果您想要管道输出,请用 OutputTypeAttribute 修饰该类。指定输出类型的属性,例如,如果您想要 Set 类型的输出对于管道,请使用 [OutputType(typeof(Set))] .
    • 为 cmdlet 的每个输入参数定义一个 C# 属性。
    • Parameter 修饰每个参数属性属性。
    • 如果您想从管道接受参数,则在使用 ParameterAttribute 进行装饰时属性,设置 ValueFromPipeline 为 true,例如 [Parameter(ValueFromPipeline =true)
    • 要向管道提供输出,请覆盖管道处理方法,例如 ProcessRecord 并使用 WriteObject 将其写入输出。
  5. 构建项目。

  6. 打开 PowerShell ISE 并运行以下代码:

    Import-Module "PATH TO YOUR BIN DEBUG FOLDER\MyCmdlets.dll"
    
    $suite = [MyCmdLets.Suite]::New("suite1")
    $suite | Add-Set "type1" "desc1"`
           | Add-Option "color1" "place1"`
           | Add-Option "color2" "place2" | Out-Null
    

    它将创建一个像这样的结构:

    Name   Sets           
    ----   ----           
    suite1 {MyCmdlets.Set}
    
    
    Type  Description Options                             
    ----  ----------- -------                             
    type1 desc1       {MyCmdlets.Option, MyCmdlets.Option}
    
    
    Color  Place 
    -----  ----- 
    color1 place1
    color2 place2
    

示例代码

模型类

如上所述,设计独立于 PowerShell 的模型类,如下所示:

using System.Collections.Generic;
namespace MyCmdlets
{
    public class Suite
    {
        public string Name { get; set; }
        public List<Set> Sets { get; } = new List<Set>();
        public Suite(string name) {
            Name = name;
        }
    }
    public class Set
    {
        public string Type { get; set; }
        public string Description { get; set; }
        public List<Option> Options { get; } = new List<Option>();
        public Set(string type, string description) {
            Type = type;
            Description = description;
        }
    }
    public class Option 
    {
        public string Color { get; set; }
        public string Place { get; set; }
        public Option(string color, string place) {
            Color = color;
            Place = place;
        }
    }
}

CmdLet 类

还根据我上面描述的注释设计 cmdlet 类:

using System.Management.Automation;
namespace MyCmdlets
{
    [Cmdlet(VerbsCommon.Add, "Set"), OutputType(typeof(Set))]
    public class AddSetCmdlet : Cmdlet
    {
        [Parameter(ValueFromPipeline = true, Mandatory = true)]
        public Suite Suite { get; set; }
        [Parameter(Position = 0, Mandatory = true)]
        public string Type { get; set; }
        [Parameter(Position = 1, Mandatory = true)]
        public string Description { get; set; }
        protected override void ProcessRecord() {
            var set = new Set(Type, Description);
            Suite.Sets.Add(set);
            WriteObject(set);
        }
    }

    [Cmdlet(VerbsCommon.Add, "Option"), OutputType(typeof(Option))]
    public class AddOptionCmdlet : Cmdlet
    {
        [Parameter(ValueFromPipeline = true, Mandatory = true)]
        public Set Set { get; set; }
        [Parameter(Position = 0, Mandatory = true)]
        public string Color { get; set; }
        [Parameter(Position = 1, Mandatory = true)]
        public string Place { get; set; }
        protected override void ProcessRecord() {
            var option = new Option(Color, Place);
            Set.Options.Add(option);
            WriteObject(Set);
        }
    }
}

关于c# - 在 C# 中创建 PowerShell Cmdlet - 管道链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58660313/

相关文章:

c# - 结构中需要重写什么以确保相等性正常运行?

c# - 在 C# Winforms 中,有没有一种方法可以在所有控件周围放置虚线边框,并在运行时选择特定控件时显示控制点?

regex - Powershell 在字符串中搜索并提取字符串的特定值

c# - 获取序列化异常 : '<>f__AnonymousType2` is not marked as serializable

powershell - Powershell,WMI方法

powershell - IX509Extension XCN编码?

powershell - 从 powershell 检查 azure 资源是否具有有效名称

Powershell不从模块导入函数

function - PowerShell - 当使用与函数相同的名称覆盖 cmdlet 的名称时,如何在函数中调用 cmdlet?

c# - 如何避免 "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection"错误?