0

Below is my code to configure required services wherein I see error as Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided. when executed.

Code should work if either of the parameters is passed else should throw error message.

param (
[Parameter(Position=0,Mandatory=$false,ParameterSetName="checks")]
[ValidateSet("srv")]
[switch]$service,
[Parameter(Position=1,Mandatory=$false,ParameterSetName="checks")]
[ValidateSet("dsk")]
[switch]$disk,
[Parameter(Position=2,Mandatory=$false,ParameterSetName="details")]
[ValidateSet("nme")]
[switch]$name)

function check_types ($service,$disk,$name) {

    if ($disk -or $service -or $name) {
      Write-Host "Argument" $name $service $disk "Provided" -ForegroundColor Green
    } else {
      Write-Host "No arguments provided" -ForegroundColor Red
    }
}

check_types -service srv -disk dsk -name nme

NKRSHNA
  • 15
  • 8
  • Yes, because you're supplying an argument to a *switch* Parameter (*type literal*). Remove any supplied values and it should work, with the exception of it falling inline with the *ParameterSet* you have declared. I.e: `check_types -service -disk`. – Abraham Zinala Jul 07 '21 at 05:26
  • Why don't you just make them mandatory? – alexzelaya Jul 07 '21 at 05:38
  • 1
    All is well-documented. Go to [About topics](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about?view=powershell-5.1) and read all articles named _about_Parameter*_ and _about_Functions*_. – JosefZ Jul 07 '21 at 05:40
  • `$name` does not share a parameter set with any of the two other parameters, so you can't pass all three at once. Remove the `ParameterSetName="..."` settings if you want to be able to pass all three at once – Mathias R. Jessen Jul 07 '21 at 08:54
  • Thanks @AbrahamZinala for the response. – NKRSHNA Jul 08 '21 at 12:07
  • @alexzelaya I want to give an option to choose according to requirement which differ. – NKRSHNA Jul 08 '21 at 12:09
  • 1
    @MathiasR.Jessen I tried removing the ```ParameterSetName``` option as suggested earlier by @AbrahamZinala and it worked out. – NKRSHNA Jul 08 '21 at 12:11

0 Answers0