I have a script where I want to call multiple parameters, which may take multiple arguments, at the same time with the format:
.\MyScript.ps1 -q <int> --args <hostval1> <hostval2> ... <hostvaln>
Right now, I have other parameters besides "q", but they do not need the "--args" parameter. I currently can't figure out how to implement "--args", because I currently have my parameters defined like so:
param(
[Alias('c')]
[string]$csv,
[Alias('h')]
[switch]$help,
[Alias('f')]
[switch]$fields,
[Alias('s')]
[string]$sql,
[Alias('q')]
[int]$query
)
I know I could run it like this to get hostvalues, but it would require a dash:
.\MyScript.ps1 -q 1 -hostval1 -hostval3 -hostval4
This makes
$args[0] = -hostval1
$args[1] = -hostval3
$args[2] = -hostval4
Is there any way to make a "secondary" parameter (e.g. --args) that can only be called with "-q", and accepts multiple values separated by spaces? I could create another parameter, but this would allow it to accidentally be called without the "-q" parameter, and also requires commas between arguments:
[string[]]$hostvals
Is what I'm looking to do possible in powershell? Does anyone have any ideas of what I could do?
[string[]]$hostvalsand use that to input the values. If you want to use it only when$queryis set, use thisif ($query) { "Query parameter present".. run code that uses $hostvals } else { run code that doesn't use $hostvals }