How to Declare Dynamic Parameters
This example shows how to define dynamic parameters that are added to the cmdlet at runtime. In this
example, the Department
parameter is added to the cmdlet whenever the user specifies the
Employee
switch parameter. For more information about dynamic parameters, see
Cmdlet Dynamic Parameters.
To define dynamic parameters
In the cmdlet class declaration, add the System.Management.Automation.Idynamicparameters interface as shown.
public class SendGreetingCommand : Cmdlet, IDynamicParameters
Call the System.Management.Automation.Idynamicparameters.Getdynamicparameters* method, which returns the object in which the dynamic parameters are defined. In this example, the method is called when the
Employee
parameter is specified.public object GetDynamicParameters() { if (employee) { context= new SendGreetingCommandDynamicParameters(); return context; } return null; } private SendGreetingCommandDynamicParameters context;
Declare a class that defines the dynamic parameters to be added. You can use the attributes that you used to declare the static cmdlet parameters to declare the dynamic parameters.
public class SendGreetingCommandDynamicParameters { [Parameter] [ValidateSet ("Marketing", "Sales", "Development")] public string Department { get { return department; } set { department = value; } } private string department; }
Example
In this example, the Department
parameter is added whenever the user specifies the Employee
parameter. The Department
parameter is an optional parameter, and the ValidateSet attribute is
used to specify the allowed arguments.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation; // PowerShell assembly.
namespace SendGreeting
{
// Declare the cmdlet class that supports the
// IDynamicParameters interface.
[Cmdlet(VerbsCommunications.Send, "Greeting")]
public class SendGreetingCommand : Cmdlet, IDynamicParameters
{
// Declare the parameters for the cmdlet.
[Parameter(Mandatory = true)]
public string Name
{
get { return name; }
set { name = value; }
}
private string name;
[Parameter]
[Alias ("FTE")]
public SwitchParameter Employee
{
get { return employee; }
set { employee = value; }
}
private Boolean employee;
// Implement GetDynamicParameters to
// retrieve the dynamic parameter.
public object GetDynamicParameters()
{
if (employee)
{
context= new SendGreetingCommandDynamicParameters();
return context;
}
return null;
}
private SendGreetingCommandDynamicParameters context;
// Override the ProcessRecord method to process the
// supplied user name and write out a greeting to
// the user by calling the WriteObject method.
protected override void ProcessRecord()
{
WriteObject("Hello " + name + "! ");
if (employee)
{
WriteObject("Department: " + context.Department);
}
}
}
// Define the dynamic parameters to be added
public class SendGreetingCommandDynamicParameters
{
[Parameter]
[ValidateSet ("Marketing", "Sales", "Development")]
public string Department
{
get { return department; }
set { department = value; }
}
private string department;
}
}
See Also
PowerShell
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기