New-Object script - need help

HI!! This is my first post and I should have signed up ages ago. Newb here =)

 

If I run this script, only the last object gets added (Senior Accountant) to the ACL. How can I fix this?

$Acl = Get-Acl “C:\MyFolder”
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule (“03NH\HR Staff”, “Modify”,“Allow”)
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule (“03NH\Budget Analyst”, “Modify”,“Allow”)
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule (“03NH\dbtest”, “Modify”,“Allow”)
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule (“03NH\Volunteer Coord”, “Modify”,“Allow”)
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule (“03NH\Admin Project Manager”, “Modify”,“Allow”)
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule (“03NH\Database Programmer”, “Modify”,“Allow”)
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule (“03NH\Finance Director”, “Modify”,“Allow”)
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule (“03NH\HR Manager”, “Modify”,“Allow”)
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule (“03NH\Office Coordinator”, “Modify”,“Allow”)
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule (“03NH\Payroll Accountant”, “Modify”,“Allow”)
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule (“03NH\Accounting Manager”, “Modify”,“Allow”)
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule (“03NH\Accounting Staff”, “Modify”,“Allow”)
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule (“03NH\Senior Accountant”, “Modify”,“Allow”)
$Acl.SetAccessRule($Ar)
Set-Acl “C:\MyFolder” $Acl

 

 

Thanks!!

Every $Ar assignment overwrites the prior one. To make all these modification you need $Acl.SetAccessRule($Ar) after every $Ar assignment

Sam you are the man!! And that makes a lot of sense now. Thank you.

A little different approach:

$rules = @()

$rules = [pscustomobject]@{
    Account = '03NH\HR Staff'
    Rights = 'Modify'
    ControlType = 'Allow'
}

$rules = [pscustomobject]@{
    Account = '03NH\Budget Analyst'
    Rights = 'Modify'
    ControlType = 'Allow'
}

$rules = [pscustomobject]@{
    Account = '03NH\dbtest'
    Rights = 'Modify'
    ControlType = 'Allow'
}

$Acl = Get-Acl "C:\MyFolder"

foreach ($rule in $acls) {
    $Ar = New-Object system.security.accesscontrol.filesystemaccessrule ($rule.Account,$rule.Rights,$rule.ControlType)
    $Acl.SetAccessRule($Ar)
}

Set-Acl "C:\MyFolder" $Acl

Hi, for the ForEach, it’s :

forEach($rule in $rules) ?
{}