srk786
srk786

Reputation: 549

how to use for loop in powershell to take multiple variable values

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'

Upvotes: 0

Views: 180

Answers (2)

Paul
Paul

Reputation: 5861

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

}

Upvotes: 0

arco444
arco444

Reputation: 22831

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

Upvotes: 1

Related Questions