PSCmdlet动态自动完成一个参数(如获取进程) [英] PSCmdlet dynamic auto complete a parameter (like Get-Process)
问题描述
在PowerShell的参数有一个动态的自动完成的行为。
为例,获取进程参数名称。我可以循环槽我用TAB的所有进程。
我想在我PSCmdlet使用这一行为。
但问题是,我只知道如何与静态自动完成valuee做到这一点。见例如:
公共类TableDynamicParameters
{
[参数]
[ValidateSet( 表1,表2)]
公共字符串[] {表得到;组; }
}
下面的例子如何与本地PowerShell中的 HTTP: //blogs.technet.com/b/heyscriptingguy/archive/2014/03/21/use-dynamic-parameters-to-populate-list-of-printer-names.aspx
它的工作原理
THX至@bouvierr
公共字符串[] {表得到;组; }
公共对象GetDynamicParameters()
{
如果返回NULL(File.Exists(路径)!);
无功表名=新的List<串GT;();
如果(TablesCache.ContainsKey(路径))
{
表名= TablesCache [路径]
}
,否则
{
试
{
表名= DbContext.GetTableNamesContent(路径);
tableNames.Add(全部);
TablesCache.Add(路径,表名);
}
赶上(例外五){}
}
变种runtimeDefinedParameterDictionary =新RuntimeDefinedParameterDictionary();
runtimeDefinedParameterDictionary.Add(表,新RuntimeDefinedParameter(表,typeof运算(字符串),新的集合<属性>(){新ParameterAttribute(),新ValidateSetAttribute(tableNames.ToArray())}));
返回runtimeDefinedParameterDictionary;
}
从MSDN:的如何声明动态参数
您 Cmdlet的
类必须实现 IDynamicParameters
接口。此接口:
提供了一种机制cmdlet来检索,可以动态地由Windows PowerShell运行时添加的参数
块引用>
编辑:
的
IDynamicParameters.GetDynamicParameters()
方法应该:
返回与参数相关的属性类似的属性和字段的对象定义一个命令类或一个RuntimeDefinedParameterDictionary对象。
块引用>
如果你看看这个的链接,笔者在PowerShell中这样做。他在运行时创建:
ValidateSetAttribute
的一个新实例的运行可能值
- 然后,他创建的阵列
RuntimeDefinedParameter
到他指定的ValidateSetAttribute
- 他返回
RuntimeDefinedParameterDictionary
包含该参数
您可以做在C#一样的。你的
GetDynamicParameters()
方法应该返回这个包含适当RuntimeDefinedParameterDictionary
RuntimeDefinedParameter
。In powershell some parameter have a dynamic auto complete behavior. For Example, Get-Process the parameter Name. I can iterate trough all my processes with TAB.
I want to use this behavior in my PSCmdlet.
But the problem is, I only know how to do this with static auto complete valuee. See the example:
public class TableDynamicParameters { [Parameter] [ValidateSet("Table1", "Table2")] public string[] Tables { get; set; } }
Here an example how this is done with native powershell http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/21/use-dynamic-parameters-to-populate-list-of-printer-names.aspx
It works thx to @bouvierr
public string[] Tables { get; set; } public object GetDynamicParameters() { if (!File.Exists(Path)) return null; var tableNames = new List<string>(); if (TablesCache.ContainsKey(Path)) { tableNames = TablesCache[Path]; } else { try { tableNames = DbContext.GetTableNamesContent(Path); tableNames.Add("All"); TablesCache.Add(Path, tableNames); } catch (Exception e){} } var runtimeDefinedParameterDictionary = new RuntimeDefinedParameterDictionary(); runtimeDefinedParameterDictionary.Add("Tables", new RuntimeDefinedParameter("Tables", typeof(String), new Collection<Attribute>() { new ParameterAttribute(), new ValidateSetAttribute(tableNames.ToArray()) })); return runtimeDefinedParameterDictionary; }
解决方案From MSDN: How to Declare Dynamic Parameters
Your
Cmdlet
class must implement theIDynamicParameters
interface. This interface:Provides a mechanism for a cmdlet to retrieve parameters that can be added dynamically by the Windows PowerShell runtime.
EDIT:
The
IDynamicParameters.GetDynamicParameters()
method should:return an object that has properties and fields with parameter related attributes similar to those define in a cmdlet class or a RuntimeDefinedParameterDictionary object.
If you look at this link, the author is doing this in PowerShell. He creates at runtime:
- a new instance of
ValidateSetAttribute
with a runtime array of possible values- He then creates a
RuntimeDefinedParameter
onto which he assigns theValidateSetAttribute
- He returns a
RuntimeDefinedParameterDictionary
containing this parameterYou can do the same in C#. Your
GetDynamicParameters()
method should return thisRuntimeDefinedParameterDictionary
containing the appropriateRuntimeDefinedParameter
.这篇关于PSCmdlet动态自动完成一个参数(如获取进程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!