Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameter sets not mandatory

Tags:

powershell

Two parameters $installDatabase and $databasePassword:

[Parameter(ParameterSetName='Test', mandatory=$false)][Switch]$installDatabase,
[Parameter(ParameterSetName='Test')][String]$databasePassword

I need this to perform the same functionality as this if:

if ($installDatabase -and !($databasePassword) -or ($databasePassword -and !($installDatabase)))
{ 
Write-Verbose "Use -installDatabase and -databasePassword together."
}

What is wrong with the ParameterSet?

like image 558
Jelphy Avatar asked Jul 30 '26 00:07

Jelphy


1 Answers

Edit: OK here is a version where the parameter set is evaluated

function test
{
    [CmdletBinding(DefaultParameterSetName="Test")]
    param(
        [Parameter(ParameterSetName='Test', mandatory=$true)]
        [switch]$installDatabase,
        [Parameter(ParameterSetName='Test2', mandatory=$true)]
        [String]$databasePassword
    )
}

In this version either you call test -installDatabase or test -databasePassword pwd or the following exception occurs:

test : Parameter set cannot be resolved using the specified named parameters.
like image 160
Micky Balladelli Avatar answered Aug 01 '26 16:08

Micky Balladelli