我在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')
Run Code Online (Sandbox Code Playgroud)
我希望能够在管道中像这样使用它:
$suite = [MyProject.SuiteBuilder]::CreateSuite('my house')
$suite | AddSet('doors', 'These represents doors') `
| AddOption('blue', 'kitchen') `
| AddOption('black', 'bedreoom') `
| AddOption('white', 'toilet')
Run Code Online (Sandbox Code Playgroud)
这是我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
而且,我正在努力使这些函数可用于管道形式中进行调用。
我还添加了一个评论,就像call this method in pipeline 在需要调用的每个评论之前一样。
简而言之,您需要:
[Parameter(ValueFromPipeline =true)] WriteObject通过调用process 方法中的方法向管道提供输出在这篇文章中,我将稍微重构您的代码,并向您展示如何在 C# 中创建Powershell Cmdlet以及如何定义参数、接受来自管道的参数以及向管道提供输出。然后你可以轻松地写出如下内容:
$suite = [MyCmdLets.Suite]::New("suite1")
$suite | Add-Set "type1" "desc1"`
| Add-Option "color1" "place1"`
| Add-Option "color2" "place2" | Out-Null
Run Code Online (Sandbox Code Playgroud)
为此,请按照下列步骤操作:
MyCmdlets)Microsoft.PowerShell.5.ReferenceAssemblies创建 cmdlet 时请考虑以下注意事项:(请参阅帖子底部的代码)
Cmdlet从类派生CmdletAttribute,例如,如果您想要Add-Set,请使用[Cmdlet(VerbsCommon.Add, "Set")]。OutputTypeAttribute指定输出类型的属性来装饰类,例如,如果您想要Set管道类型的输出,请使用[OutputType(typeof(Set))].Parameter属性装饰每个参数属性。ParameterAttribute属性装饰时,设置ValueFromPipeline为 true,例如[Parameter(ValueFromPipeline =true)ProcessRecord使用WriteObjectwrite the to output。构建项目。
打开 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
Run Code Online (Sandbox Code Playgroud)
它将创建一个像这样的结构:
Name Sets
---- ----
suite1 {MyCmdlets.Set}
Type Description Options
---- ----------- -------
type1 desc1 {MyCmdlets.Option, MyCmdlets.Option}
Color Place
----- -----
color1 place1
color2 place2
Run Code Online (Sandbox Code Playgroud)模型类
如上所述,设计独立于 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
182 次 |
| 最近记录: |