I have the following
[CmdletBinding(DefaultParametersetName='Help')]
Param (
[Parameter(ParameterSetName='Help',Mandatory=$false)]
[switch]$help,
[Parameter(ParameterSetName='ReportSilent',Mandatory=$true)]
[Parameter(ParameterSetName='UpdateSilent',Mandatory=$true)]
[switch]$silentLogin,
[Parameter(ParameterSetName='ReportSilent',Mandatory=$true)]
[Parameter(ParameterSetName='UpdateSilent',Mandatory=$true)]
[string]$apikey,
[Parameter(ParameterSetName='Report',Mandatory=$true)]
[Parameter(ParameterSetName='ReportSilent',Mandatory=$true)]
[switch]$report,
[Parameter(ParameterSetName='Report',Mandatory=$true)]
[Parameter(ParameterSetName='ReportSilent',Mandatory=$true)]
[string]$scanID,
[Parameter(ParameterSetName='Report',Mandatory=$false)]
[Parameter(ParameterSetName='ReportSilent',Mandatory=$false)]
[string]$filePath = "$PSScriptRoot\Results.csv",
[Parameter(ParameterSetName='Update',Mandatory=$true)]
[Parameter(ParameterSetName='UpdateSilent',Mandatory=$true)]
[switch]$update,
[Parameter(ParameterSetName='Update',Mandatory=$true)]
[Parameter(ParameterSetName='UpdateSilent',Mandatory=$true)]
[string]$input_file
)
If I run the script with just the -Report
switch I get an error
Parameter set cannot be resolved using the specified named parameters
I understand that the script cannot determine if it should be belong to parameter set Report
or ReportSilent
but cannot figure out how to resolve this.
My code works when the required parameters are used but I would like the user to be prompted when a mandatory parameter is missing e.g the following would prompt for apiKey
./script -report
Edit:
As noted by @denisrennes the syntaxes I am looking for are
Update: -update -input_file <string>
UpdateSilent: -update -input_file <string> -silentLogin -apikey <string>
Report: -report -scanID <string> [-filePath <string>]
ReportSilent: -report -scanID <string> [-filePath <string>] -silentLogin -apikey <string>
but I was not clear on the goal.
I am looking for a solution where essentially you have multiple default parameter sets based on the switches used. So if you use only -Report
it will default to the set Report and if you use only -Update
it will default to the set Update while still defaulting to the set help when no switches are used.
I know you can only declare a single default parameter set but am looking for a way to achieve the same result
Here are probably the syntaxes of the parameter sets you wanted:
Update: -update -input_file <string>
UpdateSilent: -update -input_file <string> -silentLogin -apikey <string>
Report: -report -scanID <string> [-filePath <string>]
ReportSilent: -report -scanID <string> [-filePath <string>] -silentLogin -apikey <string>
-Report is in Report or ReportSilent parameter set. In both cases, some mandatory arguments are missing and Powershell has to make the user enter them.
For the Report parameter set, -scanID is missing.
For the ReportSilent parameter set, -scanID and -apikey and -silentLogin are missing.
The problem is that Powershell cannot know which mandatory arguments are missing, as this depends on the parameter set.
The absence of the -SilentLogin switch does not mean here that it is the Report parameter set, as it may simply be missing here in the same way that -scanID is missing too.
If this behavior bothers you, we can do things differently, depending on your needs:
Update: -update -input_file <string>
UpdateSilent: -updateSilent -input_file <string> -apikey <string>
Report: -report -scanID <string> [-filePath <string>]
ReportSilent: -reportSilent -scanID <string> [-filePath <string>] -apikey <string>
or (but you will not be prompted for -silentLogin_apikey ...)
Update: -update -input_file <string> [-silentLogin_apikey <string>]
Report: -report -scanID <string> [-filePath <string>] [-silentLogin_apikey <string>]