silverbackbg
silverbackbg

Reputation: 182

Powershell conditions for passed parameter

I am passing a hashtable that looks like that

$table = @{
   Enabled = $true
   Name = $Name
}

I have a function which gets this object and creates an item. One of the Function's parameter is the Not Mandatory $Enabled status.

I want to create a condition that only in case the $Enabled parameter is passed to the function to accept it's content $true/$false. in other words, if I pass

$table=@{
  Name = $Name
}

it should create the object as a predefined value for Enabled - either true or false.

The condition

if($Enabled){
  do something
}

is executed either when the parameter is passed, or when it's set to true (which are desired scenarios). However, if it's set to $false (another desired scenario), it won't get into it. Thanks in advance

Upvotes: 0

Views: 123

Answers (1)

silverbackbg
silverbackbg

Reputation: 182

Found it! I set a ParameterSetName for that variable

[Parameter(Mandatory=$false,ParameterSetName="Enabled")]
[bool]$Enabled

Then inside the function the condition would look like:

if ($PSCmdlet.ParameterSetName -eq 'Enabled') {
    $object.add('Enabled',$Enabled)
}

Upvotes: 1

Related Questions