I'm trying to do something like this. The Get-SomeOtherParameter returns a system.Array type list from a database.
I don't want to hardcode my ValidateSet in case the list changes overTime in the database
function Get-SomeItems {
param (
[Parameter(Mandatory = $true)]
[ValidateSet(Get-SomeOtherParameter)]
[string]$filter,
[Parameter(Mandatory = $true)]
[ValidateSet('abc', 'def', 'ghi')]
[String]$filter2
)
}
There's two aspects to what you're trying to do:
Parameter Validation :
As you might have already noticed [ValidateSet] is a hard-coded list. It's not really possible to soft code this (it is possible to dynamically build your script every time using some other modules, lemme know if you want more of an explainer for this).
To make the Validation work without
[ValidateSet], I'd suggest[ValidateScript({})].[ValidateScript]will run whatever script is in ValidateScript to ensure the script is valid. If the [ValidateScript()] throws, the user will see that message when they pass an invalid value in.Tab-Completion :
To make it feel easy, you'll also want to add support for tab completion.
This is fairly straightforward using the
[ArgumentCompleter]attribute.Here's an example copied / pasted from a module called LightScript
This ArgumentCompleter does a few things:
Basically, all you should need to change are the command names / variables to make this work.
Hope this Helps