We help IT Professionals succeed at work.

[switch] $NoInheritance - Powershell Code

151 Views
Last Modified: 2017-04-10
Hi All,

I am trying to understand this line of code, what exactly is the [switch] $NoInheritance ? how does it work?

function DoPermissions
{
    param( $permissionGroup, $folder, $level, [switch] $NoInheritance)
}

Open in new window


Thank you in advance
Comment
Watch Question

Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
A switch is an optional parameter without (explicit) value (in fact it is a boolean). If left out when calling the function, it is $false, if you call with -NoInheritance it is $true.
You can call it with an explicit value, which you might want to do if the default value differs from $false, by e.g. using -NoInheritance:$false.
Kelly GarciaSenior Systems Administrator

Author

Commented:
ok I kind of get it, if do dopermissions --NoInheritance it will:

switch (noinheritance)
{
Pattern 1 {Statement block}
 Pattern 2 {Statement block}
 Pattern n {Statement block}
}

is this correct?
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
No. [switch] and switch are totally different kind of things. $NoInheritance just has the values $false or $true, depending on what has been provided or left out at calling the function.
Kelly GarciaSenior Systems Administrator

Author

Commented:
ok thank you, any examples to help me understand?
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION