Execute icacls in PowerShell to grant access to a file share for domain computer
Try using the call operator (&
) or cmd /c
instead of Invoke-Expression
:
& icacls $folder /grant $rule
cmd /c icacls $folder /grant $rule
or use Get-Acl
/Set-Acl
for changing permissions:
$permissions = 'Modify'$inheritance = 'ContainerInherit, ObjectInherit'$acl = Get-Acl -Path $folder$ace = New-Object Security.AccessControl.FileSystemAccessRule ($ComputerAccount, $permissions, $inheritance, 'InheritOnly', 'Allow')$acl.AddAccessRule($ace)Set-Acl -AclObject $acl -Path $folder
Invoke-Expression -Command:icacls foldername /grant groupName:"(CI)(OI)M"
This works fine. So I guess that if you will put the command into single quote (i.e. ''
) it will work. For example:
$ComputerAccount = "domain\myServer$"Invoke-Expression -Command:"icacls $ComputerAccount /grant GroupName:'(CI)(OI)M'"