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);
% is an alias for the ForEach-Object cmdlet. An alias is just another name by which you can reference a cmdlet or function.
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.
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.
Use the -bor
bitwise operator, e.g.:
([System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor
[System.Security.AccessControl.InheritanceFlags]::ObjectInherit)
ContainerInherit, ObjectInherit
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With