我正在用C#编写一个二进制Powershell模块,我想要一个带有参数的Cmdlet,它提供动态的运行时选项卡完成。但是,我正在努力弄清楚如何在二进制模块中执行此操作。以下是我尝试实现这一目标的方法:
using System;
using System.Collections.ObjectModel;
using System.Management.Automation;
namespace DynamicParameterCmdlet
{
[Cmdlet("Say", "Hello")]
public class MyCmdlet : PSCmdlet
{
[Parameter, PSTypeName("string")]
public RuntimeDefinedParameter Name { get; set; }
public MyCmdlet() : base() {
Collection<Attribute> attributes = new Collection<Attribute>() {
new ParameterAttribute()
};
string[] allowedNames = NameProvider.GetAllowedNames();
attributes.Add(new ValidateSetAttribute(allowedNames));
Name = new RuntimeDefinedParameter("Name", typeof(string), attributes);
}
protected override void ProcessRecord()
{
string name = (string)Name.Value;
WriteObject($"Hello, {Name}");
}
}
public static class NameProvider
{
public static string[] GetAllowedNames()
{
// Hard-coded array here for simplicity but imagine in reality this
// would vary at run-time
return new string[] { "Alice", "Bob", "Charlie" };
}
}
}
这不起作用。我没有任何标签完成功能。我也得到一个错误:
PS > Say-Hello -Name Alice
Say-Hello : Cannot bind parameter 'Name'. Cannot convert the "Alice" value of type "System.String" to type "System.Management.Automation.RuntimeDefinedParameter".
At line:1 char:17
+ Say-Hello -Name Alice
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Say-Hello], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,DynamicParameterCmdlet.MyCmdlet
我发现an article有一个如何在非二进制Powershell模块中执行此操作的示例。在非二进制模块中,您似乎包含DynamicParam
,后跟构建和返回RuntimeParameterDictionary
对象的语句。基于这个例子,我会期望PSCmdlet
类中的等价物,可能是一个可覆盖的GetDynamicParameters()
方法或类似的东西,就像有一个可覆盖的BeginProcessing()
方法一样。
按照这个速度,二元模块正在成为Powershell世界中的二等公民。当然有一种方法可以做到这一点我错过了吗?
答案 0 :(得分:8)
以下是在PowerShell v5中实现自定义参数完成符的方法:
Add-Type @‘
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Language;
[Cmdlet(VerbsDiagnostic.Test,"Completion")]
public class TestCompletionCmdlet : PSCmdlet {
private string name;
[Parameter,ArgumentCompleter(typeof(NameCompleter))]
public string Name {
set {
name=value;
}
}
protected override void BeginProcessing() {
WriteObject(string.Format("Hello, {0}", name));
}
private class NameCompleter : IArgumentCompleter {
IEnumerable<CompletionResult> IArgumentCompleter.CompleteArgument(string commandName,
string parameterName,
string wordToComplete,
CommandAst commandAst,
IDictionary fakeBoundParameters) {
return GetAllowedNames().
Where(new WildcardPattern(wordToComplete+"*",WildcardOptions.IgnoreCase).IsMatch).
Select(s => new CompletionResult(s));
}
private static string[] GetAllowedNames() {
return new string[] { "Alice", "Bob", "Charlie" };
}
}
}
’@ -PassThru|Select-Object -First 1 -ExpandProperty Assembly|Import-Module
特别需要:
IArgumentCompleter
界面。实现此接口的类应具有公共默认构造函数。ArgumentCompleterAttribute
属性应用于字段属性,用作cmdlet参数。作为属性的参数,您应该通过IArgumentCompleter
实现。IArgumentCompleter.CompleteArgument
中您有wordToComplete
参数,因此您可以按用户输入的文字过滤完成选项。尝试一下:
Test-Completion -Name Tab