I've been trying to search for an answer, but I think that having the words PowerShell and parameter together in a set of keywords are not making for an easy search
My question is, I'm writing a function, and I've supplied a parameter, but the parameter has to be one of a list of specific strings.
Is there a way I can supply these strings within the script so that if I type "myfunction -parameter .." I can use tab completion for the parameter value?
The PowerShell parameter is a fundamental component of any script. A parameter is a way that developers enable script users to provide input at runtime. If a PowerShell script's behavior needs to change in some way, a parameter provides an opportunity to do so without changing the underlying code.
To create a parameter set, you must specify the ParameterSetName keyword of the Parameter attribute for every parameter in the parameter set. For parameters that belong to multiple parameter sets, add a Parameter attribute for each parameter set.
To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.
If you are on PowerShell V2 you can use the [ValidateSet()]
attribute e.g.:
param( [Parameter()] [ValidateSet('foo','bar','baz')] [string[]] $Item )
See the help topic by executing:
man about_functions_advanced_parameters
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With