Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I perform a bitwise or (|) for enumerations in PowerShell?

Tags:

powershell

I am trying to perform a bitwise or (|) operation to apply multiple enumerations to a variable in PowerShell used by a .NET assembly. However, when the single pipe character to do this I get an Expressions are only allowed as the first element of a pipeline error. How do I assign multiple enumerations in PowerShell?

    $everyone = New-Object System.Security.Principal.SecurityIdentifier([System.Security.Principal.WellKnownSidType]::WorldSid, $null);
    $fsr = [System.Security.AccessControl.FileSystemRights]::Read;
    $if = [System.Security.AccessControl.InheritanceFlags]::ContainerInherit | [System.Security.AccessControl.InheritanceFlags]::ObjectInherit;
    $pf = [System.Security.AccessControl.PropagationFlags]::None;
    $act = [System.Security.AccessControl.AccessControlType]::Allow;
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($everyone, $fsr, $if, $pf, $act);
like image 490
Andrew Odri Avatar asked Jan 20 '15 18:01

Andrew Odri


People also ask

What does %% mean in PowerShell?

% is an alias for the ForEach-Object cmdlet. An alias is just another name by which you can reference a cmdlet or function.

How to use enum in PowerShell?

Create an EnumDefine a new enum using Add-Type. Once the enum type has been added to the console environment, it can not be removed or re-defined. Closing and re-opening the console session will remove the enum. In PowerShell 5.0+, the enum keyword makes this even easier.

What are the types of PowerShell operators?

There are several types of operators that can be used in PowerShell. For instance, there are operators for comparing values, operators for performing arithmetic, logical operators for comparing conditions, and, of course, operators for manipulating strings.


2 Answers

Use the -bor bitwise operator, e.g.:

([System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor
 [System.Security.AccessControl.InheritanceFlags]::ObjectInherit)

ContainerInherit, ObjectInherit
like image 179
Micky Balladelli Avatar answered Oct 11 '22 07:10

Micky Balladelli


PowerShell will provide automatic conversion from strings, so you could also do this:

$everyone = New-Object System.Security.Principal.SecurityIdentifier([System.Security.Principal.WellKnownSidType]::WorldSid, $null);
$fsr = [System.Security.AccessControl.FileSystemRights]::Read;
$if = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit,ObjectInherit"
$pf = [System.Security.AccessControl.PropagationFlags]::None;
$act = [System.Security.AccessControl.AccessControlType]::Allow;
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($everyone, $fsr, $if, $pf, $act);

In fact if you wanted it to be significantly less to type (good for quick command line stuff when you don't care about clarity for reading later) you could do it like this:

$everyone = New-Object System.Security.Principal.SecurityIdentifier([System.Security.Principal.WellKnownSidType]::WorldSid, $null);
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $everyone, "Read", "ContainerInherit,ObjectInherit", "None", "Allow"
like image 32
Mike Zboray Avatar answered Oct 11 '22 08:10

Mike Zboray