0

I am trying to create a network share and Set ACL permission, it works perfect for 1 folder but i would like to create the same thing for 1 more folder, i just do not want to repeat the code.

following is my working code for 1 folder, i will also put the path of other folder

$MediaFoler_Security = 'C:\Projects\MediaContent'
$acl = Get-Acl $MediaFoler_Security
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("IIS_IUSRS","FullControl",   
"ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
Set-Acl $MediaFoler_Security $acl

Here is my other folder path

$DeployFolder_Security = 'C:\Projects\Deployments'

2 Answers 2

1

Put it in a function:

function my-acl($path) {
  $acl = Get-Acl $path
  $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("IIS_IUSRS","FullControl",   
  "ContainerInherit, ObjectInherit", "None", "Allow")
  $acl.AddAccessRule($rule)
  Set-Acl $path $acl
}

And call like:

my-acl 'C:\Projects\MediaContent'
my-acl 'C:\Projects\Deployments'

Or:

$dirs = @('C:\Projects\MediaContent','C:\Projects\Deployments')
$dirs | % { my-acl $_ }
Sign up to request clarification or add additional context in comments.

1 Comment

More, look into Advanced Functions Get-Help about_Functions_Advanced. [CmdletBinding()] gives you -Verbose, -WhatIf and -Confirm for free - Get-Help about_functions_CmdletBinding
0

You can do it like that:

$folders = "C:\Projects\Mediacontent","C:\Projects\Deployments"

foreach($folder in $folders){

$acl = Get-Acl $folder
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("IIS_IUSRS","FullControl",   
"ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
Set-Acl $folder $acl

}

4 Comments

your code works perfect, at the moment i only have 1 user 'IIS_IUSRS', how can i add 'administrators' as well along with IIS_IUSRS without having to add an extra line,
you cant, add the extra lines for a second access rule in the loop
so is there any way i can add IIS_IUSRS and administrator in 1 go
maybe, but i cant think of one right now. also why not just add another 2 lines? if you want the logic part of your script to be organized and clean then create functions instead of doing the work in the logic part

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.