In a PowerShell function, how can I make a parameter mandatory only if another optional parameter is absent? -


i'm writing powershell function , need first parameter optional, third parameter optional only if first parameter present.

here code right now:

param(     [parameter(position=0)][array]$from,     [parameter(position=1,mandatory=$true)][string[]]$names,     [parameter(position=2)][string[]]$values ) 

ideally this:

    [parameter(position=2,mandatory=(!$from))][string[]]$values 

but not allowed.
i've been getting impression using set names in way way go, i'm not sure how go it. thing need change mandatory attribute value $values depending on existence of $from.

what best way me this?


i've looked on each of following past questions pretty thoroughly , nothing tried based on found in them work.

create function optional call variabls: powershell
requiring parameters in powershell when parameter present
powershell mandatory parameter depend on other parameter
having optional parameter requires paramter present
multiple mandatory parameter sets
conditional powershell parameters

try this:

[parameter(position=0,parametersetname = "from")] [array]$from, [parameter(position=1,mandatory=$true,parametersetname = "names")] [string[]]$names, [parameter(position=2,mandatory=$true,parametersetname = "names")] [parameter(position=2,mandatory=$false,parametersetname = "from")] [string]$values 

you define 2 sets, names , from

  • names has $names , mandatory $values
  • from has $from , optional $values

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -