I'm writing a PowerShell function and I need the first parameter to be optional, then the third parameter to be optional but only if the first parameter is present.
Here is the code as it is right now:
Param(
[Parameter(position=0)][array]$From,
[Parameter(position=1,Mandatory=$true)][string[]]$Names,
[Parameter(position=2)][string[]]$Values
)
Ideally I would do this:
[Parameter(position=2,Mandatory=(!$From))][string[]]$Values
But that is not allowed.
I've been getting the impression that using set names in some way is the way to go, but I'm not sure how I would go about it. The only thing I need to change is the Mandatory attribute value for $Values depending on the existence of $From.
What is the best way for me to do this?
I've looked over each of the following past questions pretty thoroughly and nothing I tried based on what I found in them would work.
Create a function with optional call variabls: Powershell
Requiring parameters in PowerShell when another parameter is present
PowerShell mandatory parameter depend on other parameter
Having a optional parameter that requires another paramter to be present
Multiple Mandatory Parameter Sets
Conditional powershell parameters