我正在尝试执行按位或(|)操作,以将多个枚举应用于.NET程序集使用的PowerShell中的变量。但是,当单管道符号执行此操作时,我收到Expressions are only allowed as the first element of a pipeline
错误。如何在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);
答案 0 :(得分:10)
使用-bor
[System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit
ContainerInherit, ObjectInherit
答案 1 :(得分:3)
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,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);
事实上,如果你希望它的输入要少得多(当你不关心以后阅读的清晰度时,有利于快速命令行),你可以这样做:
$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"
答案 2 :(得分:1)
这是使用-bOr
运算符完成的。请参阅TechNet documentation about ¾ down the page。
在-bOr
实现后,上面的代码示例如下所示:
$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 -bOr [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);
答案 3 :(得分:0)
您也可以将两个继承标志对象放在数组中:
$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
$if2 = [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,$if2), $pf, $act);