1

I have written a C# Tool where i can enter script parameters with an GUI which is generated based on the parmeter definitions of the script.

Now i want to have a dropdown list which offers me a dynamically generated set of values. The informations for this dropdown list should come from the parameter definition of the script.

(In my case i want to select an existing AD OU by Listing all Child-Objects of the Base OU.)

One way to get a list of valid parameters is to use "ValidateSet" for Parameter definition. There is abway to get te ValidateSet from the Script an build the dropdown list. But ValidateSet is a static deffinition and i have to update the script each time the list should be changed.

A good way for dynamic validation is "ValidateScript". The script command would be something like Test-Path. This would work for validation, but for my GUI i would not be able to generate a list of valid values.

Maby i can dynamically generate a custom enum type and use it as parameter type. A dropdown list for enum types is already implemented for GUI. But i think i's not a good idea and may not work to generate a enum type dynamically.

So, any other ideas for a list of valid values which is dynamically built?

kritzi
  • 29
  • 5

3 Answers3

0

I tried doing that with an enum once, and it got problematic due to differences in the valid character sets between enum values and AD names.

If you've wanting to keep the GUI separate from the script, you might investigate using AST to extract the parameter validation code from the script, and then run it outside the script to build your list.

mjolinor
  • 66,130
  • 7
  • 114
  • 135
0

You can use a dynamic parameter in you Powershell script. A good example a of a ValidateSet parameter attribute dynamically generated from a scriptblock and added to a dynamic parameter can be found here :

http://blogs.technet.com/b/pstips/archive/2014/06/10/dynamic-validateset-in-a-dynamic-parameter.aspx

Mathieu Buisson
  • 1,325
  • 10
  • 8
0

DynamicParam works well for PowerShell.exe.

But i have Problems to read the ValidateSet with C# Program.

Here is the Code i use:

InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] { @"C:\Users\kritzinger\OneDrive\Test-DynamicValidateSet.psm1" });
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();

PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.Commands.AddCommand("Get-Command").AddArgument("Test-DynamicValidateSet").AddParameter("ArgumentList", "Path");
Collection<PSObject> get_Command = ps.Invoke();
PSObject command = get_Command[0];
Dictionary<String, ParameterMetadata> parameters = command.Properties["parameters"].Value as Dictionary<String, ParameterMetadata>;

At the last line i get the following Exception when i try to access the Value:

An unhandled exception of type 'System.Management.Automation.GetValueInvocationException' occurred in System.Management.Automation.dll

Additional information: Exception getting "Parameters": "Cannot retrieve the dynamic parameters for the cmdlet. The pipeline has been stopped."

I get the same Exeption when i try to access the Value in VisualStudio Watch window.

With a static ValidateSet deffinition the c# code works well.

kritzi
  • 29
  • 5