Search code examples
powershellpermissionssql-granticacls

Set permissions with icacls with rules from a textfile in powershell


I'm trying to use Icacls in Powershell, to set local permissions. I have a textfile from which I get the permissions. I do it like this, because I want to change permissions of a lot of folders and I don't want to change the script every time.

Textfile looks like this (german names used):

/grant:r SYSTEM:(OI)(CI)(F) /grant:r Administratoren:(OI)(CI)(F) /grant:r mydomain\Domänen-Admins:(OI)(CI)(F) /inheritance:r
/grant:r mydomain\user1:(OI)(CI)(R) /grant:r myodomain\user2:(OI)(CI)(C)   
/grant:r mydomain\user2:(OI)(CI)(F) /deny mydomain\user2:(DE)

My code looks like this:

$AccessLine=cat .\Lines.txt
$ica=$AccessLine[0..2]
icacls.exe $path "ica"     #$path is the path of folder which should be modified

It always says invalid parameter given. I cannot write the /grant:r part in line with icacls $path because I also need to set /deny permissions. I would like to set all the permissions with just one command. Is this possible?


Solution

  • You have 3 sets of commands that need to be run. The $ica=$AccessLine[0] line works because you are only calling one of the lines. You are asking for a loop so that each line can be processed.

    $AccessLines = cat .\Lines.txt
    $AccessLines | ForEach-Object{& icacls.exe $path $_} 
    

    FWIW cat is an alias for Get-Content which returns files as arrays of lines.