Powershell:将输入管道传输到 Set-Acl

Powershell:将输入管道传输到 Set-Acl

有一个混乱的文件夹,其中的 ACL 不一致,我需要统一这些 ACL。我希望使用 powershell 来实现这一点。因此,我尝试搜索所有缺少所需组的文件,并通过 Set-Acl 应用权限:

Get-Acl | where {$_.accesstostring -notlike "*domain\required_grp*"} | Set-Acl $dir $acl 

与 icacls 不同,Set-Acl 要求您是文件的所有者才能修改 ACL。这就是原因:

$dir= Get-Acl .\reference_file
$acl= $ACL.SetOwner([System.Security.Principal.NTAccount]$my_account)

好吧,运行命令时我收到以下错误:

Set-Acl : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. At line:1 char:95

我很确定问题出在传送到 Set-Acl 的输出格式上,但不知道是什么。感谢您提供任何意见,非常感谢。

卢卡

答案1

我认为您混淆了 PowerShell,因为您向管道发送了一组 ACL,但没有对每个 ACL 进行迭代。此外,Set-ACL CMDlet 认为需要向管道发送路径。

$acl= Get-Acl .\reference_file
$acl= $ACL.SetOwner([System.Security.Principal.NTAccount]$my_account)
Get-ChildItem | ? { (Get-ACL $_).accesstostring -notlike "*domain\groupname*"} | % { Set-ACL $_.Fullname $acl }

这对我来说很管用。它获取每个文件或文件夹,检查其 ACL 是否符合您的表达式,并根据需要设置 ACL。

相关内容