Powershell: Piping input to Set-Acl

access-control-listpowershell

have a messed up folder with inconsistent ACLs which I need to uniform. I hoped to accomplish this with powershell. So I'm trying to search for all files missing the required group and applying the rights threough Set-Acl:

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

Unlike icacls, Set-Acl requires you to be the owner of the file to be able to modify the ACL. That's why:

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

Well, when running the command I get the following error:

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

I'm pretty sure the problem is with the format of the output piped to Set-Acl, but can't figure out what. Thanks for any input, much appreciated.

Luka

Best Answer

I believe you are confusing the PowerShell, because you send an array of ACLs down the pipeline, but don't iterate over each of them. Also, the Set-ACL CMDlet expects a path sent down the pipeline, it think.

$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 }

This works for me. It gets every file or folder, checks if it's ACL matches your expression, and sets the ACL if required.