I am working on preparing PowerShell cmdlet and have to set unique combinations for the inputs such that at a time only a given combination can be applied otherwise it should throw parameter set cannot be resolved using the specified named parameters.
public abstract class ServerCmdletBase
{
public const string IdParameterSetName = "Id";
public const string InputObjectParameterSetName = "InputObject";
[Parameter(Mandatory = true, ParameterSetName = IdParameterSetName)]
public int[] Id { get; set; }
[Parameter(Mandatory = true, ParameterSetName = InputObjectParameterSetName)]
public string[] InputObject { get; set; }
...
...
}
public class EditServerCmdlet : ServerCmdletBase
{
public const string ConnectionStringParameterSetName = "ConnectionString";
public const string PasswordParameterSetName = "Password";
[Parameter(Mandatory = true, ParameterSetName = ConnectionStringParameterSetName)]
[Parameter(ParameterSetName = IdParameterSetName )]
[Parameter(ParameterSetName = InputObjectParameterSetName )]
public string ConnectionString { get; set; }
[Parameter(Mandatory = true, ParameterSetName = PasswordParameterSetName)]
[Parameter(ParameterSetName = IdParameterSetName )]
[Parameter(ParameterSetName = InputObjectParameterSetName )]
public string Username { get; set; }
[Parameter(Mandatory = true, ParameterSetName = PasswordParameterSetName)]
[Parameter(ParameterSetName = IdParameterSetName )]
[Parameter(ParameterSetName = InputObjectParameterSetName )]
public string Password { get; set; }
...
...
}
The end goal is -Id and -InputObject should not be used at the same time. For the given input (Id or InputObject), -ConnectionString and -Username, -Password should not be used at the same time. Hence total 4 unique combinations.
I am using ParameterSetName here but can't get it to work. The above code only works for Id and InputObject, it can't handle ConnectionString/Username & Password. Please advise for the right approach. Thanks a lot!