PSCmdlet dynamic auto complete a parameter (like Get-Process)

不想你离开。 提交于 2019-12-06 13:50:13

From MSDN: How to Declare Dynamic Parameters

Your Cmdlet class must implement the IDynamicParameters 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 the ValidateSetAttribute
  • He returns a RuntimeDefinedParameterDictionary containing this parameter

You can do the same in C#. Your GetDynamicParameters() method should return this RuntimeDefinedParameterDictionary containing the appropriate RuntimeDefinedParameter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!